#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#define int long long
using namespace std;
int cost[101][101];
int d[101];
bool used[101];
int V,k,o,a,b,c,di,e,ans;
void dij(int s){
    fill(d,d+101,10000000);
    fill(used,used+101,false);
    d[s]=0;
    while(true){
        int v=-1;
        for(int u=0;u<V;u++){
            if(!used[u]&&(v==-1||d[u]<d[v]))v=u;
        }
        if(v==-1)break;
        used[v]=true;
        for(int u=0;u<V;u++){
            d[u]=min(d[u],d[v]+cost[v][u]);
        }
    }
}
signed main(){
    for(;;){
        cin>>V>>k;
        if(V==0&&k==0)break;
        fill(cost[0],cost[0]+101*101,10000000);
        for(int i=0;i<k;i++){
            cin>>o;
            if(o==1){
                cin>>c>>di>>e;
                cost[c-1][di-1]=min(cost[c-1][di-1],e);
                cost[di-1][c-1]=min(cost[di-1][c-1],e);
            }
            if(o==0){
                cin>>a>>b;
                dij(a-1);
                if(d[b-1]==10000000)cout<<-1<<endl;
                else cout<<d[b-1]<<endl;
            }
        }
    }
    return 0;
}
#include<iostream>
using namespace std;

int main(){
while(1){
	int INF=100000100;
	int n,k,ans[105][105],t,a,b,c;
	scanf("%d%d",&n,&k);
	if(n==0&&k==0)break;
	for(int i=1;i<=n+2;i++){
		for(int j=1;j<=n+2;j++){
			ans[i][j]=INF;
			if(i==j)ans[i][j]=0;
		}
	}
	for(int i=0;i<k;i++){
		scanf("%d",&t);
		if(t==0){
			scanf("%d%d",&a,&b);
			if(ans[a][b]==INF)printf("%d\n",-1);
			else printf("%d\n",ans[a][b]);
		}
		if(t==1){
			scanf("%d%d%d",&a,&b,&c);
			if(ans[a][b]>c){
				ans[a][b]=c;
				ans[b][a]=c;
				for(int i=1;i<=n;i++){
					for(int j=1;j<=n;j++){
						if(ans[i][j]>ans[i][a]+ans[a][b]+ans[b][j])ans[i][j]=ans[i][a]+ans[a][b]+ans[b][j];
						if(ans[i][j]>ans[i][b]+ans[b][a]+ans[a][j])ans[i][j]=ans[i][b]+ans[b][a]+ans[a][j];
					}
				}
			}
		}
	}
}
}