#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
const long long INF = 1LL << 60;    // 1.15x10^18
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using edge = pair<int, long long>;
using Graph = vector<vector<edge>>;

vector<long long> dijkstra(const Graph &g, int s) {
    vector<long long> dist(g.size(), INF);
    using Pi = pair<long long, int>;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    dist[s] = 0; que.emplace(dist[s], s);
    while (!que.empty()) {
        long long cost; int u; tie(cost, u) = que.top(); que.pop();
        if (dist[u] < cost) continue;
        for (auto &e: g[u]) {
            int v; long long nc; tie(v, nc) = e;
            if (chmin(dist[v], dist[u] + nc)) que.emplace(dist[v], v);
        }
    }
    return dist;
}

int main() {
    int n, k;
    while (cin >> n >> k, n) {
        Graph g(n);
        while (k--) {
            int com; cin >> com;
            if (com == 0) {
                int a, b; cin >> a >> b; a--, b--;
                auto res = dijkstra(g, a)[b];
                cout << (res == INF ? -1 : res) << endl;
            } else if (com == 1) {
                int c, d, e; cin >> c >> d >> e; c--, d--;
                g[c].emplace_back(d, e);
                g[d].emplace_back(c, e);
            }
        }
    }
    return 0;
}

#include<stdio.h>
#define INF 100000001
int n,m;
int i,j,k;
int t[100][100];
int r,a,b,c;
bool used[100];
int d[100];
int ans;

int min(int a,int b){
	if(a<=b)return a;
	else return b;
}

int solve(int a,int b){
	int v;
	while(1){
		v=-1;
		for(k=0;k<n;k++)if(!used[k]&&(v==-1||d[k]<d[v]))v=k;
		if(d[v]==INF||v==-1)break;
		used[v]=true;
		for(k=0;k<n;k++){
			if(t[v][k]!=INF)d[k]=min(d[k],d[v]+t[v][k]);
		}
	}
	return d[b];
}
int main(){
	while(1){
		scanf("%d %d",&n,&m);
		if(n==0&&m==0)return 0;
		for(i=0;i<n;i++)for(j=0;j<n;j++)t[i][j]=INF;
		for(i=0;i<m;i++){
			scanf("%d",&r);
			if(r==1){
				scanf("%d %d %d",&a,&b,&c);
				t[a-1][b-1]=min(t[a-1][b-1],c);
				t[b-1][a-1]=min(t[b-1][a-1],c);
			}
			if(r==0){
				scanf("%d %d",&a,&b);
				for(j=0;j<n;j++){
					used[j]=false;
					d[j]=INF;
				}
				d[a-1]=0;
				ans=solve(a-1,b-1);
				if(ans==INF)printf("-1\n");
				else printf("%d\n",ans);
			}
		}
	}
}