#include<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<numeric>

#define MOD 1000000007

using namespace std;
typedef long long ll;

ll dist[110][110];
const ll INF = 100000000;
int N, K;


void update(int a, int b) {
	for (int k =0 ; k <N; k++) {
		for (int i = 0; i < N; i++) {
			for (int j = 0; j <N;j++) {
				dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
			}
		}
	}
}

int main()
{
	while (1) {
		cin >> N >> K;
		if (N == 0 && K == 0)break;

		for (int i = 0; i < N; i++)
			for (int j = 0; j < N; j++)
				dist[i][j] = INF;

		for (int i = 0; i < K; i++) {
			ll a, b, c, d;
			cin >> a;
			if (a == 1) {
				cin >> b >> c >> d;
				b--, c--;
				if (dist[b][c] > d) {
					dist[b][c] = dist[c][b] = d;
					update(b, c);
				}
			}
			else if (a == 0) {
				cin >> b >> c;
				b--, c--;
				if (dist[b][c] != INF)
					cout << dist[b][c] << endl;
				else
					cout << -1 << endl;
			}
		}
	}
	return 0;
}