#include<bits/stdc++.h>
using namespace std;
inline int scan(){
char c = getchar_unlocked();
int x = 0;
while(c<'0'||c>'9'){
c=getchar_unlocked();
}
while(c>='0'&&c<='9'){
x=(x<<1)+(x<<3)+c-'0';
c=getchar_unlocked();
}
return x;
}
#define author rajat1603
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define N 201
#define inf INT_MAX-2
vector<pair<int,int> > v[N];
priority_queue<pair<int,int> > q;
int PREV[N];
int dist[N];
vector<pair<int,int> > lolgraph[N];
int original[N];
int PREW[N];
int main(){
memset(original,-1,sizeof(original));
int n=scan(),m=scan();
while(m--){
int a=scan(),b=scan(),c=scan();
v[a].pb(mp(b,c));
v[b].pb(mp(a,c));
}
int ans=1;
int ansdist=inf;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[j]=inf;
PREV[j]=-1;
lolgraph[j].clear();
PREW[j]=-1;
}
dist[i]=0;
q.push(mp(0,i));
while(!q.empty()){
int node=q.top().second;
q.pop();
int x=v[node].size();
while(x--){
int next=v[node][x].first;
int weight=v[node][x].second;
if(dist[next]>dist[node]+weight){
dist[next]=dist[node]+weight;
q.push(mp(-dist[next],next));
PREV[next]=node;
PREW[next]=weight;
}
}
}
bool b=0;
for(int j=1;j<=n;j++){
if(dist[j]==inf){
b=1;
break;
}
if(PREV[j]!=-1){
lolgraph[PREV[j]].pb(mp(j,PREW[j]));
lolgraph[j].pb(mp(PREV[j],PREW[j]));
}
}
if(b)continue;
int maxdistnode=i;
queue<int> bfs;
bfs.push(i);
int tempdist[N]={0};
bool visited[N]={0};
while(!bfs.empty()){
int node=bfs.front();
if(tempdist[node]>tempdist[maxdistnode]){
maxdistnode=node;
}
visited[node]=1;
bfs.pop();
int x=lolgraph[node].size();
while(x--){
int next=lolgraph[node][x].first;
int weight=lolgraph[node][x].second;
if(next!=node){
if(!visited[next]){
bfs.push(next);
tempdist[next]=tempdist[node]+weight;
}
}
}
}
bool vis[N]={0};
int tempd[N]={0};
bfs.push(maxdistnode);
int maximum=0;
while(!bfs.empty()){
int node=bfs.front();
maximum=max(maximum,tempd[node]);
bfs.pop();
vis[node]=1;
int x=lolgraph[node].size();
while(x--){
int next=lolgraph[node][x].first;
int weight=lolgraph[node][x].second;
if(next!=node){
if(!vis[next]){
bfs.push(next);
tempd[next]=tempd[node]+weight;
}
}
}
}
if(maximum<ansdist){
ansdist=maximum;
for(int j=1;j<=n;j++){
original[j]=PREV[j];
}
}
}
printf("%d\n",ansdist);
for(int i=1;i<=n;i++){
if(original[i]!=-1){
printf("%d %d\n",original[i],i);
}
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW5saW5lIGludCBzY2FuKCl7CiAgICBjaGFyIGMgPSBnZXRjaGFyX3VubG9ja2VkKCk7CiAgICBpbnQgeCA9IDA7CiAgICB3aGlsZShjPCcwJ3x8Yz4nOScpewogICAgICAgIGM9Z2V0Y2hhcl91bmxvY2tlZCgpOwogICAgfQogICAgd2hpbGUoYz49JzAnJiZjPD0nOScpewogICAgICAgIHg9KHg8PDEpKyh4PDwzKStjLScwJzsKICAgICAgICBjPWdldGNoYXJfdW5sb2NrZWQoKTsKICAgIH0KICAgIHJldHVybiB4Owp9CiNkZWZpbmUgYXV0aG9yIHJhamF0MTYwMwojZGVmaW5lIG1vZCAxMDAwMDAwMDA3CiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgTiAyMDEKI2RlZmluZSBpbmYgSU5UX01BWC0yCnZlY3RvcjxwYWlyPGludCxpbnQ+ID4gdltOXTsKcHJpb3JpdHlfcXVldWU8cGFpcjxpbnQsaW50PiA+IHE7CmludCBQUkVWW05dOwppbnQgZGlzdFtOXTsKdmVjdG9yPHBhaXI8aW50LGludD4gPiBsb2xncmFwaFtOXTsKaW50IG9yaWdpbmFsW05dOwppbnQgUFJFV1tOXTsKaW50IG1haW4oKXsKICAgIG1lbXNldChvcmlnaW5hbCwtMSxzaXplb2Yob3JpZ2luYWwpKTsKICAgIGludCBuPXNjYW4oKSxtPXNjYW4oKTsKICAgIHdoaWxlKG0tLSl7CiAgICAgICAgaW50IGE9c2NhbigpLGI9c2NhbigpLGM9c2NhbigpOwogICAgICAgIHZbYV0ucGIobXAoYixjKSk7CiAgICAgICAgdltiXS5wYihtcChhLGMpKTsKICAgIH0KICAgIGludCBhbnM9MTsKICAgIGludCBhbnNkaXN0PWluZjsKICAgIGZvcihpbnQgaT0xO2k8PW47aSsrKXsKICAgICAgICBmb3IoaW50IGo9MTtqPD1uO2orKyl7CiAgICAgICAgICAgIGRpc3Rbal09aW5mOwogICAgICAgICAgICBQUkVWW2pdPS0xOwogICAgICAgICAgICBsb2xncmFwaFtqXS5jbGVhcigpOwogICAgICAgICAgICBQUkVXW2pdPS0xOwogICAgICAgIH0KICAgICAgICBkaXN0W2ldPTA7CiAgICAgICAgcS5wdXNoKG1wKDAsaSkpOwogICAgICAgIHdoaWxlKCFxLmVtcHR5KCkpewogICAgICAgICAgICBpbnQgbm9kZT1xLnRvcCgpLnNlY29uZDsKICAgICAgICAgICAgcS5wb3AoKTsKICAgICAgICAgICAgaW50IHg9dltub2RlXS5zaXplKCk7CiAgICAgICAgICAgIHdoaWxlKHgtLSl7CiAgICAgICAgICAgICAgICBpbnQgbmV4dD12W25vZGVdW3hdLmZpcnN0OwogICAgICAgICAgICAgICAgaW50IHdlaWdodD12W25vZGVdW3hdLnNlY29uZDsKICAgICAgICAgICAgICAgIGlmKGRpc3RbbmV4dF0+ZGlzdFtub2RlXSt3ZWlnaHQpewogICAgICAgICAgICAgICAgICAgIGRpc3RbbmV4dF09ZGlzdFtub2RlXSt3ZWlnaHQ7CiAgICAgICAgICAgICAgICAgICAgcS5wdXNoKG1wKC1kaXN0W25leHRdLG5leHQpKTsKICAgICAgICAgICAgICAgICAgICBQUkVWW25leHRdPW5vZGU7CiAgICAgICAgICAgICAgICAgICAgUFJFV1tuZXh0XT13ZWlnaHQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgYm9vbCBiPTA7CiAgICAgICAgZm9yKGludCBqPTE7ajw9bjtqKyspewogICAgICAgICAgICBpZihkaXN0W2pdPT1pbmYpewogICAgICAgICAgICAgICAgYj0xOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYoUFJFVltqXSE9LTEpewogICAgICAgICAgICAgICAgbG9sZ3JhcGhbUFJFVltqXV0ucGIobXAoaixQUkVXW2pdKSk7CiAgICAgICAgICAgICAgICBsb2xncmFwaFtqXS5wYihtcChQUkVWW2pdLFBSRVdbal0pKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZihiKWNvbnRpbnVlOwogICAgICAgIGludCBtYXhkaXN0bm9kZT1pOwogICAgICAgIHF1ZXVlPGludD4gYmZzOwogICAgICAgIGJmcy5wdXNoKGkpOwogICAgICAgIGludCB0ZW1wZGlzdFtOXT17MH07CiAgICAgICAgYm9vbCB2aXNpdGVkW05dPXswfTsKICAgICAgICB3aGlsZSghYmZzLmVtcHR5KCkpewogICAgICAgICAgICBpbnQgbm9kZT1iZnMuZnJvbnQoKTsKICAgICAgICAgICAgaWYodGVtcGRpc3Rbbm9kZV0+dGVtcGRpc3RbbWF4ZGlzdG5vZGVdKXsKICAgICAgICAgICAgICAgIG1heGRpc3Rub2RlPW5vZGU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdmlzaXRlZFtub2RlXT0xOwogICAgICAgICAgICBiZnMucG9wKCk7CiAgICAgICAgICAgIGludCB4PWxvbGdyYXBoW25vZGVdLnNpemUoKTsKICAgICAgICAgICAgd2hpbGUoeC0tKXsKICAgICAgICAgICAgICAgIGludCBuZXh0PWxvbGdyYXBoW25vZGVdW3hdLmZpcnN0OwogICAgICAgICAgICAgICAgaW50IHdlaWdodD1sb2xncmFwaFtub2RlXVt4XS5zZWNvbmQ7CiAgICAgICAgICAgICAgICBpZihuZXh0IT1ub2RlKXsKICAgICAgICAgICAgICAgIAlpZighdmlzaXRlZFtuZXh0XSl7CiAgICAgICAgICAgICAgICAgICAgICAgIGJmcy5wdXNoKG5leHQpOwogICAgICAgICAgICAgICAgICAgICAgICB0ZW1wZGlzdFtuZXh0XT10ZW1wZGlzdFtub2RlXSt3ZWlnaHQ7CiAgICAgICAgICAgICAgICAJfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGJvb2wgdmlzW05dPXswfTsKICAgICAgICBpbnQgdGVtcGRbTl09ezB9OwogICAgICAgIGJmcy5wdXNoKG1heGRpc3Rub2RlKTsKICAgICAgICBpbnQgbWF4aW11bT0wOwogICAgICAgIHdoaWxlKCFiZnMuZW1wdHkoKSl7CiAgICAgICAgICAgIGludCBub2RlPWJmcy5mcm9udCgpOwogICAgICAgICAgICBtYXhpbXVtPW1heChtYXhpbXVtLHRlbXBkW25vZGVdKTsKICAgICAgICAgICAgYmZzLnBvcCgpOwogICAgICAgICAgICB2aXNbbm9kZV09MTsKICAgICAgICAgICAgaW50IHg9bG9sZ3JhcGhbbm9kZV0uc2l6ZSgpOwogICAgICAgICAgICB3aGlsZSh4LS0pewogICAgICAgICAgICAgICAgaW50IG5leHQ9bG9sZ3JhcGhbbm9kZV1beF0uZmlyc3Q7CiAgICAgICAgICAgICAgICBpbnQgd2VpZ2h0PWxvbGdyYXBoW25vZGVdW3hdLnNlY29uZDsKICAgICAgICAgICAgICAgIGlmKG5leHQhPW5vZGUpewogICAgICAgICAgICAgICAgCWlmKCF2aXNbbmV4dF0pewogICAgICAgICAgICAgICAgICAgICAgICBiZnMucHVzaChuZXh0KTsKICAgICAgICAgICAgICAgICAgICAgICAgdGVtcGRbbmV4dF09dGVtcGRbbm9kZV0rd2VpZ2h0OwogICAgICAgICAgICAgICAgCX0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZihtYXhpbXVtPGFuc2Rpc3QpewogICAgICAgICAgICBhbnNkaXN0PW1heGltdW07CiAgICAgICAgICAgIGZvcihpbnQgaj0xO2o8PW47aisrKXsKICAgICAgICAgICAgICAgIG9yaWdpbmFsW2pdPVBSRVZbal07CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBwcmludGYoIiVkXG4iLGFuc2Rpc3QpOwogICAgZm9yKGludCBpPTE7aTw9bjtpKyspewogICAgICAgIGlmKG9yaWdpbmFsW2ldIT0tMSl7CiAgICAgICAgICAgIHByaW50ZigiJWQgJWRcbiIsb3JpZ2luYWxbaV0saSk7CiAgICAgICAgfQogICAgfQp9Cgo=