#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <queue>
#include <functional>
using namespace std;
int main()
{
	int n,k;
	int node[101][101];
	while(cin>>n>>k,n|k){
	
		for(int i=0; i<=n; i++)
			for(int j=0; j<=n; j++)
				node[i][j] = INT_MAX;
		
		while(k--){
			int m;
			cin>>m;
			if( m ){
				int c,d,e;
				cin>>c>>d>>e;
				node[c][d] = node[d][c] = min(node[c][d],e);
			}else{
				int a,b;
				cin>>a>>b;
				/*
				for(int i=1; i<=n; i++){
					for(int j=1; j<=n; j++)
						printf("%13d,",node[i][j]);
					puts("");
				}*/
				int memo[101];
				priority_queue< pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq;
				for(int i=0; i<=n; i++)memo[i]=INT_MAX;
				pq.push(make_pair(0,a));
				memo[a]=0;
				while( !pq.empty() ){
					pair<int,int> pi = pq.top();
					if( pi.second == b )break;
					pq.pop();
					for(int i=1; i<=n; i++){
						if( node[pi.second][i]!=INT_MAX &&
							 node[pi.second][i]+pi.first < memo[i] ){
							memo[i] = node[pi.second][i] + pi.first;
							pq.push(make_pair(memo[i],i));
						}
					}
				}
				printf("%d\n",memo[b]==INT_MAX?-1:memo[b]);
			}
		}
	}
	return 0;
}