#include<stdio.h> #include<string.h> int father[105],depth[105]; int dist[105],map[101][101]; int vis[105],n; void init_B() { int i; for(i = 1;i <= n;i ++) { father[i] = i; depth[i] = 0; } } int find(int x) { if(x == father[x]) return x; return father[x] = find(father[x]); } void unit(int x,int y) { x = find(x); y = find(y); if(x == y) return ; if(depth[x] < depth[y]) father[x] = y; else { if(depth[x] > depth[y]) father[y] = x; else { father[x] = y; depth[y]++; } } } void init() { int i; memset(vis,0,sizeof(vis)); for(i = 1;i <= n;i ++) dist[i] = map[1][i]; } int main() { int m,i,j,k,a,b; int min,cnt,cost,sum; while(~scanf("%d%d",&m,&n) && m) { init_B(); sum = cnt = 0; for(i = 1;i <= n;i ++) { for(j = 1;j <= n;j ++) { if(i != j) { map[i][j] = 1 << 30; } } } while(m--) { scanf("%d%d%d",&a,&b,&cost); map[a][b] = map[b][a] = cost; unit(a,b); } init(); for(i = 1;i <= n;i ++) { if(i == find(i)) cnt++; if(cnt == 2) break ; } if(cnt == 2) { printf("? "); continue ; } for(i = 0;i < n;i ++) { min = 1 << 30; for(j = 1;j <= n;j ++) { if(!vis[j] && min > dist[j]) { min = dist[j]; k = j; } } vis[k] = 1; if(min != 1 << 30) sum += min; for(j = 1;j <= n;j ++) { if(!vis[j] && dist[j] > map[k][j]) dist[j] = map[k][j]; } } printf("%d ",sum); } return 0; }