#include<iostream>
#include<queue>
#include<vector>
#include<functional>

using namespace std;

int n, k;
int dis[100];
bool f = false;
int nowdis[100];
vector<vector<pair<int,int>>> edge;//to cost

int djk(int start, int end) {
	priority_queue < pair<int, int>, vector < pair<int, int>>, greater<pair<int, int>>> pq;// cost pos
	for (int i = 0; i < n; i++) {
		dis[i] = 100000000;
	}
	pq.push(make_pair(0, start));
	while (pq.size()) {
		pair<int, int> tmp = pq.top();
		pq.pop();
		if (tmp.first < dis[tmp.second]) {
			dis[tmp.second] = tmp.first;
			for (int i = 0; i < edge[tmp.second].size(); i++) {
				pq.push(make_pair(tmp.first + edge[tmp.second][i].second, edge[tmp.second][i].first));
			}
		}
	}
	if (dis[end] == 100000000)return -1;
	return dis[end];
}

int main() {
	while (cin >> n >> k, n * k) {
		edge.resize(n);
		for (int i = 0; i < k; i++) {
			int a;
			cin >> a;
			if (a == 0) {
				int b, c;
				cin >> b >> c;
				cout << djk(b - 1, c - 1) << endl;
			}
			else {
				int b, c, d;
				cin >> b >> c >> d;
				edge[b - 1].push_back(make_pair(c - 1, d));
				edge[c - 1].push_back(make_pair(b - 1, d));
			}
		}
		edge.clear();
	}
	return 0;
}