#include<bits/stdc++.h>
#define rep(i, a, n) for (int i=a;i<=n;i++)
#define per(i, a, n) for (int i=a;i>=n;i--)
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e3;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
int n, m;
int graph[maxn][maxn];
int lowcost[maxn], closest[maxn];
int sum;
void Prim(int s) {
rep(i, 1, n) {
if (i == s)
lowcost[i] = 0;
else
lowcost[i] = graph[s][i];
closest[i] = s;
}
int minn, pos;
sum = 0;
rep(i, 1, n) {
minn = inf;
rep(j, 1, n) {
if (lowcost[j] != 0 && lowcost[j] < minn) {
minn = lowcost[j];
pos = j;
}
}
if (minn == inf)break;
sum += minn;
lowcost[pos] = 0;
rep(j, 1, n) {
if (lowcost[j] != 0 && graph[pos][j] < lowcost[j]) {
lowcost[j] = graph[pos][j];
closest[j] = pos;
}
}
}
cout << sum << endl;
}
void Print(int s) {
int temp;
rep(i, 1, n) {
if (i != s) {
temp = closest[i];
cout << temp << "->" << i << "边权值为:" << graph[temp][i] << endl;
}
}
}
int main() {
freopen("C:\\Users\\30414\\Desktop\\LuoguWeek3\\input.txt", "r", stdin);
IOS;
while (cin >> n >> m) {
memset(graph, inf, sizeof(graph));
int u, v, w;
rep(i, 1, m) {
cin >> u >> v >> w;
graph[u][v] = graph[v][u] = w;
}
Prim(1);
Print(1);
}
return 0;
}