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.
public class p17B {
    public static void main(String[] args) {
        int n=nextInt();
        for(int i=0;i<n;i++) nextInt(); 
        int m=nextInt();
        edge list[]=new edge[m];
        for(int i=0;i<m;i++) list[i]=new edge(nextInt(),nextInt(),nextInt());
        System.out.println(kruskalMST(list, n));
    }
    static int kruskalMST(edge[] edgelist,int n) {
        int sum=0;
        java.util.Arrays.parallelSort(edgelist);
        dsu d=new dsu(n);
        for(edge e:edgelist) {
            if(d.connected(e.u-1, e.v-1)||d.id[e.v-1]!=e.v-1) continue;
            d.unite(e.u-1, e.v-1);
            sum+=e.w;
        }
        int z=d.find(0);
        for(int i=0;i<n;i++) if(z!=d.find(i)) return -1;
        return sum;
    }
    static class dsu{
        int sz,components,size[],id[];
        dsu(int n) {
            this.sz=components=n;
            size=new int[n];
            id=new int[n];
            for(int i=0;i<n;i++) {
                size[i]=1;
                id[i]=i;
            }
        }
        int find(int p) {
            int root=p;
            while(root!=id[root]) root=id[root];
            while(p!=root) {
                int nxt=id[p];
                id[p]=root;
                p=nxt;
            }
            return root;
        }
        boolean connected(int a,int b) {
            return find(a)==find(b);
        }
        void unite(int a,int b) {
            int root1=find(a),root2=find(b);
            if(root1==root2) return;
            if(size[root1]<size[root2]) {
                id[root1]=root2;
            }else {
                id[root2]=root1;
            }
            if(size[root1]==size[root2]) size[root2]++;
            components--;
        }
        int size() {
            return sz;
        }
        int components() {
            return components;
        }
        int componentSize(int a) {
            return size[find(a)];
        }
    }
    static java.io.InputStream in=System.in;
    static int nextInt(){
        int x=0,p=1,c=0;
        try {do c=in.read(); while(c<=32);
        if(c=='-') {p=-1;c=in.read();}
        for(;c>='0'&&c<='9';c=in.read()) x=x*10+c-'0';
        }catch(Throwable e) {}
        return x*p;
    }
    static class edge implements Comparable<edge>{
        int u,v,w;
        edge(int... a){u=a[0];v=a[1];w=a[2];}
        public int compareTo(edge o) {
            return Integer.compare(w,o.w);
        }
    }
}