Problem: Masha is fond of cacti. When she was a little girl, she decided to plant a tree. Now Masha wants to make a nice cactus out of her tree.

Recall that tree is a connected undirected graph that has no cycles. Cactus is a connected undirected graph such that each vertex belongs to at most one cycle.

Masha has some additional edges that she can add to a tree. For each edge she knows which vertices it would connect and the beauty of this edge. Masha can add some of these edges to the graph if the resulting graph is a cactus. Beauty of the resulting cactus is sum of beauties of all added edges. 

Help Masha find out what maximum beauty of the resulting cactus she can achieve.

Input

The first line of the input data contains two integers n and m — the number of vertices in a tree, and the number of additional edges available (3 ≤ n ≤ 2·105; 0 ≤ m ≤ 2·105).

Let us describe Masha's tree. It has a root at vertex 1. The second line contains n - 1 integers: p2, p3, ..., pn, here pi — is the parent of a vertex i — the first vertex on a path from the vertex i to the root of the tree (1 ≤ pi < i).

The following m lines contain three integers ui, vi and ci — pairs of vertices to be connected by the additional edges that Masha can add to the tree and beauty of edge (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ ci ≤ 104).

It is guaranteed that no additional edge coincides with the edge of the tree.

Output

Output one integer — the maximum beauty of a cactus Masha can achieve.

Example

Input

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


Output

2

Solution (in JAVA):

Solution:
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.io.*;
/*
7 3
1 1 2 2 3 3
4 5 1
6 7 1
2 3 1
 */
public class d {
	public static void main(String[] arg) throws IOException {
		new d();
	}
	ArrayList<Integer>[] tree;
	ArrayList<Edge>[] edges;
	// notInclude means dont include the edges
	// included means to include the new edges
	HeavyLight notIncluded, included;
	int[] memo;
	public d() throws IOException {
		FastScanner in = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = in.nextInt();
		int m = in.nextInt();
		tree = new ArrayList[n];
		edges = new ArrayList[n];
		memo = new int[n];
		for(int i = 0; i < n; i++) {
			tree[i] = new ArrayList<Integer>();
			edges[i] = new ArrayList<Edge>();
		}
		for(int i = 1; i < n; i++) {
			int p = in.nextInt()-1;
			tree[i].add(p);
			tree[p].add(i);
		}
		LCA_BL bl = new LCA_BL(tree, 0);
		for(int i = 0; i < m; i++) {
			Edge e = new Edge(in.nextInt()-1, in.nextInt()-1, in.nextInt());
			int lca = bl.lca(e.a, e.b);
			edges[lca].add(e);
		}
		notIncluded = new HeavyLight(tree, true);
		included = new HeavyLight(tree, true);
		ArrayDeque<Integer> stack = new ArrayDeque<Integer>();
		ArrayDeque<Integer> q = new ArrayDeque<Integer>();
		q.add(0);
		q.add(0);
		while(q.size() > 0) {
			int v = q.poll();
			int p = q.poll();
			stack.push(p);
			stack.push(v);
			for(int to : tree[v]) {
				if(to == p) continue;
				q.add(to);
				q.add(v);
			}
		}
		while(stack.size() > 0) {
			int v = stack.pop();
			int p = stack.pop();
			int op1 = 0;
			int op2 = 0;
			for(int to : tree[v]) {
				if(to == p) continue;
				op1 += memo[to];
			}
			notIncluded.modify(v, v, op1);
			for(Edge e : edges[v]) {
				op2 = Math.max(op2, e.w-included.query(e.a, e.b)+notIncluded.query(e.a, e.b));
			}
			int ret = Math.max(op1, op2);
			included.modify(v, v, ret);
			memo[v] = ret;
		}
		out.println(memo[0]);
		in.close(); out.close();
	}
	class Edge {
		int a, b, w;
		public Edge(int a, int b, int w) {
			this.a = a;
			this.b = b;
			this.w = w;
		}
	}
	class LCA_BL 
	{
		int n;
		int[] pars;
		int[][] bl;
		int[] depth;
		int[] deg;
		ArrayList<Integer>[] adj;
		public LCA_BL(ArrayList<Integer>[] a, int root)
		{
			adj = a;
			n = a.length;
			pars = new int[n];
			bl = new int[32][n];
			depth = new int[n];
			deg = new int[n];
			dagify(root);
			lcaprecomp();
		}
		void dagify(int root)
		{
			Arrays.fill(pars, -1);
			Arrays.fill(deg, 0);
			pars[root] = root;
			depth[root] = 0;
			ArrayDeque<Integer> q = new ArrayDeque<Integer>();
			q.add(root);
		   	while(q.size() > 0)
		   	{
		   		int v = q.pop();
		   		for(int to : adj[v])
		   		{
		   			if(to == pars[v]) continue;
		         	pars[to] = v;
		         	depth[to] = depth[v]+1;
		         	q.add(to);
		         	deg[v]++;
		   		}
		   	}
		   	pars[root] = -1;
		}
		void lcaprecomp()
		{
			for(int i = 0; i < n; i++) bl[0][i] = pars[i];
			for(int i = 1; i < 32; i++)
			{
				for(int j = 0; j < n; j++)
				{
					if(bl[i-1][j] == -1) bl[i][j] = -1;
					else bl[i][j] = bl[i-1][bl[i-1][j]];
				}
			}
		}
		int walk(int x, int dist)
		{
			for(int i = 31; i >= 0; i--) if(((1<<i)&dist) != 0) x = bl[i][x];
		   	return x;
		}
		int lca(int x, int y)
		{
			if(depth[x] < depth[y]) y = walk(y, depth[y]-depth[x]);
		   	if(depth[x] > depth[y]) x = walk(x, depth[x]-depth[y]);
		   	if(x == y) return x;
		   	for(int i = 31; i >= 0; i--)
		   	{
		   		if(bl[i][x] != bl[i][y])
		   		{
		   			x = bl[i][x];
		   			y = bl[i][y];
		   		}
		   	}
		   	return bl[0][x];
		}
	}
	
	// Heavy-light decomposition with path queries. Query complexity is O(log^2(n)).
	// Based on the code from http://codeforces.com/blog/entry/22072
	public class HeavyLight {

	  int getNeutralValue() {
	    return 0;
	  }

	  final List<Integer>[] tree;
	  final boolean valuesOnVertices; // true - values on vertices, false - values on edges
	  final BIT segmentTree;
	  final int[] parent;
	  final int[] heavy;
	  final int[] depth;
	  final int[] pathRoot;
	  final int[] pos;

	  public HeavyLight(List<Integer>[] tree, boolean valuesOnVertices) {
	    this.tree = tree;
	    this.valuesOnVertices = valuesOnVertices;
	    int n = tree.length;
	    segmentTree = new BIT(n);

	    parent = new int[n];
	    heavy = new int[n];
	    depth = new int[n];
	    pathRoot = new int[n];
	    pos = new int[n];

	    Arrays.fill(heavy, -1);
	    parent[0] = -1;
	    depth[0] = 0;
	    dfs(0);
	    for (int u = 0, p = 0; u < n; u++) {
	      if (parent[u] == -1 || heavy[parent[u]] != u) {
	        for (int v = u; v != -1; v = heavy[v]) {
	          pathRoot[v] = u;
	          pos[v] = p++;
	        }
	      }
	    }
	  }

	  int dfs(int u) {
	    int size = 1;
	    int maxSubtree = 0;
	    for (int v : tree[u]) {
	      if (v != parent[u]) {
	        parent[v] = u;
	        depth[v] = depth[u] + 1;
	        int subtree = dfs(v);
	        if (maxSubtree < subtree) {
	          maxSubtree = subtree;
	          heavy[u] = v;
	        }
	        size += subtree;
	      }
	    }
	    return size;
	  }

	  public int query(int u, int v) {
	    AtomicInteger res = new AtomicInteger(getNeutralValue()); // just mutable integer
	    processPath(u, v, (a, b) -> res.set(res.get()+segmentTree.query(a, b)));
	    return res.get();
	  }

	  public void modify(int u, int v, int delta) {
	    processPath(u, v, (a, b) -> segmentTree.update(a, delta));
	  }

	  void processPath(int u, int v, BiConsumer<Integer, Integer> op) {
	    for (; pathRoot[u] != pathRoot[v]; v = parent[pathRoot[v]]) {
	      if (depth[pathRoot[u]] > depth[pathRoot[v]]) {
	        int t = u;
	        u = v;
	        v = t;
	      }
	      op.accept(pos[pathRoot[v]], pos[v]);
	    }
	    if (!valuesOnVertices && u == v) return;
	    op.accept(Math.min(pos[u], pos[v]) + (valuesOnVertices ? 0 : 1), Math.max(pos[u], pos[v]));
	  }
	  class BIT {
		  int[] vs;
		  public BIT(int s) {
			  vs = new int[s+2];
		  }
		  public void update(int i, int v) {
			  i++;
			  for(;i < vs.length; i += (i&-i)) vs[i] += v;
		  }
		  public int sum(int i) {
			  i++;
			  int ret = 0;
			  for(;i > 0; i -= (i&-i)) ret += vs[i];
			  return ret;
		  }
		  public int query(int a, int b) {
			  return sum(b)-sum(a-1);
		  }
	  }
	}
	class FastScanner {
		BufferedReader br;
		StringTokenizer st;
		public FastScanner(InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
			st = new StringTokenizer("");
		}
		public String next() throws IOException {
			while(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public void close() throws IOException {
			br.close();
		}
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
	}
}