#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "math.h"
#include "utility"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "random"

using namespace std;
const long long int MOD = 1000000007;

int N, K;
int a, b, c, d;

int main(){
	cin >> N>>K;
	while (N) {
		int cost[101][101] = {};
		for (int i = 0; i < K; i++) {
			cin >> a;
			if (a) {
				cin >> b >> c >> d;
				if (cost[b][c])cost[b][c] = min(cost[b][c], d);
				else cost[b][c] = d;
				cost[c][b] = cost[b][c];
			}
			else {
				cin >> b >> c;
				queue<int>Q;
				int dis[101];
				for (int j = 0; j <= N; j++) {
					dis[j] = INT_MAX;
				}
				dis[b] = 0;
				Q.push(b);
				while (!Q.empty()) {
					for (int j = 1; j <= N; j++) {
						if (!cost[Q.front()][j])continue;
						if (dis[Q.front()] + cost[Q.front()][j] >= dis[j])continue;
						dis[j] = dis[Q.front()] + cost[Q.front()][j];
						Q.push(j);
					}
					Q.pop();
				}
				if (dis[c] == INT_MAX)cout << "-1\n";
				else cout << dis[c] << endl;
			}
		}
		cin >> N>>K;
	}
	return 0;
}