#include <cstdio>
#include <algorithm>

using namespace std;

#define chmin(a,b) a=min(a,b)
#define rep(i,x) for(int i=0;i<(x);++i)

const int inf = 1e9;

int d[100][100];

int main()
{
	int N, K;
	
	while (scanf("%d %d", &N, &K), N || K) {
		fill(d[0], d[N], inf);
		rep(i, N) d[i][i] = 0;

		rep(i, K) {
			int t; scanf("%d", &t);
			if (t) {
				int a, b, c; scanf("%d %d %d", &a, &b, &c); a--; b--;
				d[a][b] = d[b][a] = min(d[a][b], c);
				rep(j, N) rep(k, N) {
					chmin(d[j][k], min(d[j][a] + d[a][b] + d[b][k], d[j][b] + d[b][a] + d[a][k]));
				}
			} else {
				int a, b; scanf("%d %d", &a, &b); a--; b--;
				printf("%d\n", d[a][b] != inf ? d[a][b] : -1);
			}
		}
	}
}