#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <functional>
using namespace std;

typedef pair<int, int> P;

const int INF = 1e+9;
int N, d[101];
vector<P> G[101];

int dijkstra(int s, int t){
	fill(d, d + N + 1, INF);
	d[s] = 0;
	priority_queue<P, vector<P>, greater<P> > que;
	que.push(P(0, s));
	while(!que.empty()){
		P p = que.top();
		que.pop();
		if(d[p.second] < p.first)
			continue;
		for(P e : G[p.second]){
			if(d[e.first] > d[p.second] + e.second){
				d[e.first] = d[p.second] + e.second;
				que.push(P(d[e.first], e.first));
			}
		}
	}
	return d[t];
}

int main() {
	int k, t;
	
	while(cin >> N >> k && (N || k)){
		for(int i = 0; i <= 100; ++i)
			G[i].clear();
			
		for(int i = 0; i < k; ++i){
			scanf("%d", &t);
			if(t == 1){
				int c, d, e;
				scanf("%d%d%d", &c, &d, &e);
				G[c].push_back(P(d, e));
				G[d].push_back(P(c, e));
			}
			else{
				int a, b;
				scanf("%d%d", &a, &b);
				int ans = dijkstra(a, b);
				if(ans == INF)
					cout << "-1" << endl;
				else
					cout << ans << endl;
			}
		}
	}
	return 0;
}