#include <iostream>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)

const int inf = 1 << 28;

int main(void){
	int n,k;
	while(cin >> n >> k,n){
		int dist[110][110];
		rep(i,n)rep(j,n) dist[i][j]=inf;
		rep(i,n) dist[i][i]=0;
		rep(loop,k){
			int type;
			cin >> type;
			if(type){
				int a,b,c;
				cin >> a >> b >> c;
				a--,b--;
				dist[a][b]=min(dist[a][b],c);
				dist[b][a]=min(dist[b][a],c);
				rep(i,n)rep(j,n) dist[i][j]=min(dist[i][j],dist[i][a]+dist[a][j]);
				rep(i,n)rep(j,n) dist[i][j]=min(dist[i][j],dist[i][b]+dist[b][j]);
			}else{
				int a,b;
				cin >> a >> b;
				a--,b--;
				if(dist[a][b]==inf)
					cout << -1 << endl;
				else
					cout << dist[a][b] << endl;
			}
		}
	}
}