using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using static System.Math;
using System.Text.RegularExpressions;

namespace CsharpSpiral {
	partial class Program {
		static void Main(string[] args) {
			var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
			Console.SetOut(sw);
			new Program().Solve(new ConsoleInput(Console.In, ' '));
			Console.Out.Flush();
		}
		public void Solve(ConsoleInput cin) {
			var V = cin.ReadInt;
			var G = new Graph(V);
			var K = cin.ReadInt;
			for(int i = 0; i < K; i++) {
				var line = cin.ReadIntArray(3);
				G.AddEdge(line[0], line[1], line[2]);
				G.AddEdge(line[1], line[0], line[2]);
			}
			Graph T;
			Console.WriteLine(G.Prim(out T));
		}
	}

	static class ExtendClass {
		public static string Join<T>(this T[] X, string separator = " ") => string.Join(separator, X);
	}

	class ConsoleInput {
		private readonly TextReader _stream;
		private readonly char _separator = ' ';
		private readonly Queue<string> inputStream;
		public ConsoleInput(TextReader stream, char separator = ' ') {
			this._separator = separator;
			this._stream = stream;
			inputStream = new Queue<string>();
		}
		public string Read {
			get {
				if(inputStream.Count != 0) return inputStream.Dequeue();
				string[] tmp = _stream.ReadLine().Split(_separator);
				for(int i = 0; i < tmp.Length; ++i)
					inputStream.Enqueue(tmp[i]);
				return inputStream.Dequeue();
			}
		}
		public string ReadLine { get { return _stream.ReadLine(); } }
		public int ReadInt { get { return int.Parse(Read); } }
		public long ReadLong { get { return long.Parse(Read); } }
		public double ReadDouble { get { return double.Parse(Read); } }
		public string[] ReadStrArray(long N) {
			var ret = new string[N];
			for(long i = 0; i < N; ++i)
				ret[i] = Read;
			return ret;
		}
		public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
		public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
		public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i < N; ++i) ret[i] = ReadDouble; return ret; }
	}

	class PriorityQueue<T> where T : IComparable<T> {
		protected readonly List<T> body = new List<T>();
		protected Comparer<T> comparer { get; set; }
		public PriorityQueue(bool maxTop = true) {
			comparer
			= maxTop ? Comparer<T>.Create((T i, T j) => i.CompareTo(j))
			: Comparer<T>.Create((T i, T j) => j.CompareTo(i));
		}
		public PriorityQueue(Comparer<T> mComparer) { comparer = mComparer; }
		//suppose Me>Her if isRootMaximum
		protected int Compare(T Me, T Her) => comparer.Compare(Me, Her);
		protected int Compare(int x, int y) => Compare(body[x], body[y]);
		protected virtual void Swap(int x, int y) { T tmp = body[x]; body[x] = body[y]; body[y] = tmp; }
		public virtual void Push(T elem) {
			body.Add(elem);
			//the parent of n-th element is the ((n+1)>>1)-1-th
			UpHeap(lastIndex);
		}
		public void Push(IEnumerable<T> seq) {
			foreach(var item in seq) {
				Push(item);
			}
		}
		protected int UpHeap(int j) {
			while(j > 0) {
				int parent = ((j + 1) >> 1) - 1;
				if(Compare(parent, j) < 0) {
					Swap(parent, j);
				} else break;
				j = parent;
			}
			return j;
		}
		public virtual T Pop() {
			T top = body[0];
			Swap(0, lastIndex);
			body.RemoveAt(lastIndex);
			DownHeap(0);
			return top;
		}
		protected int DownHeap(int j) {
			while(j < length) {
				var leftChild = ((j + 1) << 1) - 1;
				if(leftChild >= length) break;
				var toSwap = leftChild;
				var rightChild = leftChild + 1;
				if(rightChild < length && Compare(leftChild, rightChild) < 0)
					toSwap = rightChild;
				if(Compare(j, toSwap) < 0) {
					Swap(toSwap, j);
				} else break;
				j = toSwap;
			}
			return j;
		}
		public T Top { get { return body[0]; } }
		public int length { get { return body.Count(); } }
		protected int lastIndex { get { return body.Count() - 1; } }
		public bool Any() => body.Any();
		public string Emit() { return body.ToArray().Join(","); }
	}
	class Edge<V>: IComparable<Edge<V>> where V : IComparable<V> {
		public int In { get; set; }
		public int Out { get; set; }
		public V cost { get; set; }
		public int CompareTo(Edge<V> y) => cost.CompareTo(y.cost);
	}
	class Node<V>: IComparable<Node<V>> where V : IComparable<V> {
		public V Value { get; set; }
		public int index { get; set; }
		public int CompareTo(Node<V> y) => Value.CompareTo(y.Value);
	}
	class Graph: Graph<long> {
		public Graph(int count) : base(count) { }
		public long Prim(out Graph tree, int start = 0) {
			tree = new Graph(N);
			var res = 0L;
			var PQ = new PriorityQueue<Edge<long>>(false);
			var O = Outs;
			PQ.Push(O[start]);
			var picked = new bool[N];
			picked[start] = true;
			while(PQ.Any()) {
				var e = PQ.Pop();
				var w = e.Out;
				if(picked[w]) continue;
				else picked[w] = true;
				res += e.cost;
				tree.Edges.Add(e);
				PQ.Push(O[w]);
			}
			return res;
		}
	}
	class Graph<V> where V : IComparable<V> {
		public int N { get; }
		public Graph(int count) {
			N = count;
			Edges = new List<Edge<V>>();
		}
		public List<Edge<V>> Edges { get; }
		public void AddEdge(int In, int Out, V Value) {
			var e = new Edge<V> { In = In, Out = Out, cost = Value };
			Edges.Add(e);
		}
		protected List<Edge<V>>[] Outs {
			get {
				var Res = new List<Edge<V>>[N];
				for(int i = 0; i < N; i++)
					Res[i] = new List<Edge<V>>();
				foreach(var e in Edges)
					Res[e.In].Add(e);
				return Res;
			}
		}
		protected List<Edge<V>>[] Ins {
			get {
				var Res = new List<Edge<V>>[N];
				for(int i = 0; i < N; i++)
					Res[i] = new List<Edge<V>>();
				foreach(var e in Edges)
					Res[e.Out].Add(e);
				return Res;
			}
		}
		

	}
}


Measured memory_bytes: 42108