using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static void Main(){
		Sc sc=new Sc();
		int[] s=sc.Ia;
		int n=s[0],m=s[1],o=n;
		var li=new List<Tuple<int,int>>[n*2];
		int[] h=new int[n*2],p=new int[n*2],r=new int[n*2],g=new int[n*2];
		for(int i=0;i<n;i++){
			li[i]=new List<Tuple<int,int>>();
			g[i]=i;
			h[i]=-1;
		}
		for(int i=0;i<m;i++){
			int[] e=sc.Ia;
			li[e[1]].Add(Tuple.Create(e[0],e[2]));
		}
		long ans=0;
		for(int i = 0;i<=o;i++) {
			if(g[i]!=i){continue;}
			int v=i;
			while(h[v]==-1&&v!=s[2]){
				int u2=-1,uj=0;
				for(int j=1;j<li[v].Count;j++){
					if(li[v][uj].Item2>li[v][j].Item2){uj=j;}
				}
				u2=li[v][uj].Item1;
				while(u2!=g[u2]){u2=g[u2];}
				g[li[v][uj].Item1]=u2;
				p[v]=u2;
				r[v]=li[v][uj].Item2;
				h[v]=i;
				ans+=li[v][uj].Item2;
				v=u2;
				if(i==h[v]){
					li[o]=new List<Tuple<int,int>>();
					g[o]=o;
					h[o]=-1;
					int u=p[v];
					while(u!=g[u]){u=g[u];}
					g[p[v]]=u;
					while(g[u]!=o){
						g[u]=o;
						for(int j=0;j<li[u].Count;j++){
							if(li[u][j].Item1!=p[u]){
								int x=li[u][j].Item1;
								while(x!=g[x]){x=g[x];}
								g[li[u][j].Item1]=x;
								if(x!=o){li[o].Add(Tuple.Create(x,li[u][j].Item2-r[u]));}
							}
						}
						u=p[u];
						int q=u;
						while(u!=g[u]){u=g[u];}
						g[q]=u;
					}
					li[o]=li[o].GroupBy(z=>z.Item1).Select(x=>x.Min()).ToList();
					v=o;
					o++;
				}
			}
		}
		Console.WriteLine("{0}",ans);
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}



Measured memory_bytes: 18756