#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
using namespace std;

const int INF=100000000;
int warshall_floyd(int a,int b,vector< vector<int> >& vi,int V);

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,k;
	while(cin>>n>>k&&n!=0&&k!=0){
		vector< vector<int> > wf(n+1,vector<int>(n+1));
		for(int i=1; i<n+1; ++i){
			for(int j=1; j<n+1; ++j){
				if(i==j){
					wf[i][j]=0;
				} else {
					wf[i][j]=INF;
				}
			}
		}
		for(int i=0; i<k; ++i){
			int s;
			cin >> s;
			if(s==0){
				int a,b;
				cin >> a >> b;
				if(wf[a][b]<INF){
					cout << wf[a][b] << "\n";
				} else {
					cout << -1 << "\n";
				}
			} else if(s==1){
				int c,d,e;
				cin >> c >> d >> e;
				if(e<wf[c][d]){
					wf[c][d]=e;
					wf[d][c]=e;
					warshall_floyd(1,1,wf,n);
				}
			}
		}
	}
	return 0;
}

int warshall_floyd(int a,int b,vector< vector<int> >& vi,int V){
	for(int k=1; k<=V; ++k){
		for(int i=1; i<=V; ++i){
			for(int j=1; j<=V; ++j)
				vi[i][j]=min(vi[i][j],vi[i][k]+vi[k][j]);
		}
	}
	return vi[a][b];
}