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

int n, l;
int c[100][100];
const int INF = 10001000;

void wf(int x, int y){
	for(int i=0; i<n; i++)
		for(int j=0; j<n; j++){
			c[i][j] = min(c[i][j], c[i][x] + c[x][y] + c[y][j]);
			c[i][j] = min(c[i][j], c[i][y] + c[y][x] + c[x][j]);
		}
}

int main(){
	while(1){
		scanf("%d %d", &n, &l);
		if(n == 0 && l == 0) break;
		for(int i=0; i<100; i++)
			for(int j=0; j<100; j++)
				c[i][j] = INF;
		for(int i=0; i<n; i++)
			c[i][i] = 0;
		for(int h=0; h<l; h++){
			int t;
			scanf("%d", &t);
			if(t == 0){
				int a, b;
				scanf("%d %d", &a, &b);
				if(c[a-1][b-1] == INF) printf("-1\n");
				else printf("%d\n", c[a-1][b-1]);
			}else{
				int x, y, z;
				scanf("%d %d %d", &x, &y, &z);
				c[x-1][y-1] = min(c[x-1][y-1], z);
				c[y-1][x-1] = c[x-1][y-1];
				wf(x-1, y-1);
			}
		}
	}
	return 0;
}