/**
 *    author:  orzvanh14 ( Độc cô cầu đặc )
 *    created: 18.04.2026 03:56:02
 *    too lazy to update time
**/
// i wants to take ioi
//binhtinhtutinkhongcaycunhungmotkhikhongcontutinnualatuyetvong
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define nn "\n"
#define pi pair<int, int>
#define ti tuple<int, int, int>
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define eb emplace_back
#define pb push_back
#define TASK " "

#define ms(a, x) memset(a, x, sizeof(a))
#define all(a) a.begin(), a.end()
#define All(a, n) a + 1, a + 1 + n

#define LOG 19

const int INF = 1e18;
const int N = 2e4 + 5;
const int maxn = 100 + 5;
const int mod = 1e9 + 7;


struct node{
	int kc, u;
	bool operator<(const node& other) const {
        return kc > other.kc;
    }
};
struct edge{
	int u, v, w, id;
	bool operator<(const edge& other) const {
        return w > other.w;
    }
};
edge edges[N];
int n, m, k;
int sz[N];
int par[N];
int p[N];
void make_sets(int s){
    sz[s] = 1;
    par[s] = s;
}
int get(int a){
    if(a == par[a]) return a;
    return par[a] = get(par[a]);
}
bool union_sets(int a, int b){
    a = get(a);
    b = get(b);
    if(a != b){
        if(sz[a] < sz[b]){
            // sz[a] > sz[b]
            swap(a, b);
        }
        sz[a] += sz[b];
        par[b] = a;
        return 1;
    }
    return 0;
}
void nhap(){
	cin >> m >> n;
	for(int i = 0; i < m; i++){
        int x, y, w;
        cin >> x >> y >> w;
        edges[i].u = x;
        edges[i].v = y;
        edges[i].w =  w;
        edges[i].id = i + 1;
    }
	sort(edges, edges + m);
}
void solve(){
	int ans =0;
	vector<int> res;
	for(int i = 1; i <= n; i++) make_sets(i);
	for(int i= 0; i < m; i++){
        int u = edges[i].u;
        int v = edges[i].v;
        int w = edges[i].w;
        int idx = edges[i].id;
        if(union_sets(u, v)){
            ans += w;
            res.pb(idx);
        }

	}
	cout << ans << nn;
	for(int x : res){
        cout << x << nn;
	}
}
signed main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	nhap();
	solve();
	return 0;
}
