#include <cstdio>
#include <algorithm>
using namespace std;

const int INF = 1000000000;

int main(){

while(1){

int n,k;

int r[102][102] = {0};

scanf("%d%d",&n,&k);

if(n == 0) break;

for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(i == j){
r[i][j] = 0;
}else{
r[i][j] = INF;
}
}
}

for(int i = 0;i < k;i++){
	int f;
	scanf("%d",&f);
	if(f == 0){
	int a,b;
	scanf("%d%d",&a,&b);
	if(r[a][b] == INF){
	printf("-1\n");
	}else{
	printf("%d\n",r[a][b]);
	}
	}else{
	int c,d,e;
	scanf("%d%d%d",&c,&d,&e);
	if(r[c][d] == INF || r[c][d] > e){
	r[c][d] = e; r[d][c] = e;
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= n;j++){
			if(i == j) continue;
			r[i][j] = min(r[i][j],r[i][c]+e+r[d][j]);
			r[i][j] = min(r[i][j],r[i][d]+e+r[c][j]);
		}
	}
	}
	}
}
}
return 0;


}
			
