1 #include <iostream>
2 #include <cstdio>
3 #include <algorithm>
4 #define N 110
5 #define M 5000
6 using namespace std;
7
8 int n, m, u[M], v[M], w[M], r[M], p[N];
9 bool cmp(int i, int j){ return w[i]<w[j]; }
10 int f(int x){ return p[x] == x ? x : p[x] = f(p[x]); }
11
12 int main()
13 {
14 while (cin >> m >> n&&m)
15 {
16 for (int i = 1; i <= n; ++i) p[i] = i;
17 for (int j = 1; j <= m; ++j) r[j] = j;
18 for (int j = 1; j <= m; ++j) cin >> u[j] >> v[j] >> w[j];
19 sort(r + 1, r + 1 + m, cmp);
20 int ans = 0, k = 0;
21 for (int j = 1; j <= m&&k<n - 1; ++j)
22 {
23 int e = r[j]; int x = f(u[e]), y = f(v[e]);
24 if (x != y)ans += w[e], ++k, p[x] = y;
25 }
26 if (k<n - 1)puts("?");
27 else cout << ans << endl;
28 }
29 return 0;
30 }