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.InputMismatchException;
import java.util.Arrays;

public class Main {
    static InputStream inStream;
    static PrintWriter out = new PrintWriter(System.out);
    static String INPUT = "";
    static int roots = 0;
    static int employees;
    static int[][] adjacency;
    static int[][] cost; //cost of all incoming edges
    static int totalCost=0;
    static boolean[] visited;

    public static void dfs(int v){
        visited[v]=true;

        for(int nextV=1; nextV<=employees; nextV++){
            if(adjacency[v][nextV]==2 && visited[nextV]==false){
                dfs(nextV);
            }
        }

        //find minimum cost to get this employee a manager
        int parents=0;
        int minCost=1000000;
        for(int prevV=1; prevV<=employees; prevV++) {
            if(adjacency[v][prevV]==1) {
                parents++;
                    if(cost[v][prevV]<minCost) {
                        minCost = cost[v][prevV];
                    }
            }
        }
        if(parents==0) {
            roots++;
            minCost=0;
        }

        totalCost += minCost;

    }

    public static void main(String[] args) throws IOException {
        inStream = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        employees = nextInt();
        adjacency = new int[employees+1][employees+1];
        cost = new int[employees+1][employees+1];
        visited = new boolean[employees+1];

        int[]  q = nextArray1(employees);
        int appNum = nextInt();

        for(int i=0; i<appNum; i++){
            int[] app = nextArray1(3);

            //crate adjacency matrix
            //1 represents incoming edge and 2 is outgoing
            if(adjacency[app[1]][app[2]]==0){
                adjacency[app[1]][app[2]] = 2;
                adjacency[app[2]][app[1]] = 1;
                cost[app[2]][app[1]] = app[3];
            } else {
                if(cost[app[2]][app[1]]>app[3]){
                    cost[app[2]][app[1]]=app[3];
                }
            }
        }

        for(int i=1; i<=employees; i++){
            if(!visited[i]){
                dfs(i);
            }
        }

        if(roots>1)
            out.println(-1);
        else
            out.println(totalCost);

        out.flush();
    }


    private static boolean eof() {
        if (lenbuf == -1) return true;
        int lptr = ptrbuf;
        while (lptr < lenbuf) if (!isSpaceChar(inbuf[lptr++])) return false;
        try {
            inStream.mark(1000);
            while (true) {
                int b = inStream.read();
                if (b == -1) {
                    inStream.reset();
                    return true;
                } else if (!isSpaceChar(b)) {
                    inStream.reset();
                    return false;
                }
            }
        } catch (IOException e) {
            return true;
        }
    }

    private static byte[] inbuf = new byte[1024];
    static int lenbuf = 0, ptrbuf = 0;

    private static int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = inStream.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0)
                return -1;
        }
        return inbuf[ptrbuf++];
    }

    private static boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private static int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private static double nextDouble() {
        return Double.parseDouble(nextString());
    }

    private static char nextChar() {
        return (char) skip();
    }

    private static String nextString() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private static StringBuilder nextStringBuilder() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb;
    }

    private static char[] nextString(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private static int[] nextArray0(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    private static int[] nextArray1(int n) {
        int[] a = new int[n + 1];
        for (int i = 1; i <= n; i++)
            a[i] = nextInt();
        return a;
    }

    private static long[] nextArrayLong1(int n) {
        long[] a = new long[n + 1];
        for (int i = 1; i <= n; i++)
            a[i] = nextLong();
        return a;
    }

    private static long[] nextArrayLong0(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    }

    private static int nextInt() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private static long nextLong() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}
