Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi. 

Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.

Input

The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.

Output

Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.

Examples

Input

4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5


Output

11


Input

3
1 2 3
2
3 1 2
3 1 3


Output

-1

Note

In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
import java.io.*;
import java.util.*;

public class Main {
    private static PrintWriter out;
    private static Scanner in;

    private static Edge[] G;

    static class Edge implements Comparable<Edge> {
        public int from, to, weight;
        public Edge(int u, int v, int w) {
            from = u;
            to = v;
            weight = w;
        }

        public Edge(Edge e) {
            from = e.from;
            to = e.to;
            weight = e.weight;
        }

        @Override
        public int compareTo(Edge e) {
            if (weight < e.weight)
                return -1;
            else if (weight == e.weight)
                return 0;
            else
                return 1;
        }
    }

    public static class DisjointSetUnion<T> {
        private Dictionary<T, T> parents;
        private Dictionary<T, Integer> size;

        public DisjointSetUnion() {
            parents = new Hashtable<T, T>();
            size = new Hashtable<T, Integer>();
        }

        public void makeSet(T newElement) {
            parents.put(newElement, newElement);
            size.put(newElement, 1);
        }

        public T findSet(T element) {
            if (element.equals(parents.get(element)))
                return element;
            T leader = findSet(parents.get(element));
            parents.put(element, leader);
            return leader;
        }

        public void unionSets(T firstElement, T secondElement) {
            T a = findSet(firstElement),
              b = findSet(secondElement);
            if (!a.equals(b)) {
                if (size.get(a) < size.get(b)) {
                    T t = a;
                    a = t;
                    t = b;
                }
                parents.put(b, a);
                size.put(a, size.get(a) + size.get(b));
            }
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        in = new Scanner(System.in);
        out = new PrintWriter(System.out);

        int n = in.nextInt();
        for (int i = 0; i < n; ++i)
            in.next();
        int m = in.nextInt();

        G = new Edge[m];
        for (int i = 0; i < m; ++i) {
            int from = in.nextInt(),
                to = in.nextInt(),
                weight = in.nextInt();
            G[i] = new Edge(from, to, weight);
        }
        /*int[] treeID = new int[n];
        for (int i = 0; i < n; ++i)
            treeID[i] = i; */

        boolean[] isBossed = new boolean[n];
        Arrays.fill(isBossed, false);
        int totalWeight = 0;
        Arrays.sort(G);
        DisjointSetUnion<Integer> dsu = new DisjointSetUnion<Integer>();
        for (int i = 0; i < n; ++i)
            dsu.makeSet(i);
        for (Edge e : G) {
            int from = e.from - 1,
                to = e.to - 1;
            if (!isBossed[to] && dsu.findSet(from) != dsu.findSet(to)) {
                totalWeight += e.weight;
                dsu.unionSets(from, to);
                isBossed[to] = true;
            }
        }
        boolean isMSTFound = true;
        int firstSetLeader = dsu.findSet(0);
        for (int i = 1; i < n; ++i) {
            if (firstSetLeader != dsu.findSet(i)) {
                isMSTFound = false;
                break;
            }
        }
        if (isMSTFound)
            out.println(totalWeight);
        else
            out.println(-1);

        out.flush();
    }
} 
