#define _USE_MATH_DEFINES
#define INF 100000000
#include <bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;

static const double EPS = 1e-8;

int path[101][101];

int dijkstra(int s,int g,int n){
	int d[101];
	bool used[101];
	memset(d,0x3f,sizeof(d));
	memset(used,0,sizeof(used));
	d[s] = 0;
	
	while(1){
		int v=-1;
		for(int u=1;u<=n;u++){
			if(!used[u] && (v==-1 || d[u] < d[v])) v=u;
		}

		if(v==-1) break;

		used[v] = true;

		for(int u=1;u<=n;u++){
			d[u] = min(d[u],d[v] + path[v][u]);
		}
	}

	return d[g];
}

int main(){
	int n,k;

	while(~scanf("%d %d",&n,&k)){
		if(n==k && k==0) break;
		memset(path,0x3f,sizeof(path));
		for(int i=0;i<k;i++){
			int ch;
			scanf("%d",&ch);
			if(ch){
				int a,b,val;
				scanf("%d %d %d",&a,&b,&val);
				path[a][b] = path[b][a] = min(val,path[a][b]);
			}
			else{
				int a,b,res;
				scanf("%d %d",&a,&b);
				res = dijkstra(a,b,n);
				printf("%d\n",res > INF ? -1 : res);

			}
		}
	}
}
#include <bits//stdc++.h>
using namespace std;
int main(void){
    
    while(1){
        long long i,j,a,b,c,k,l;
        cin>>a>>b;
        if(a==0&&b==0)
        break;
        long long island[a][a];
        for(i=0;i<a;i++){
            for(j=0;j<a;j++){
                if(i==j)
                    island[i][j]=0;
                    
                else
                    island[i][j]=LLONG_MAX/2;
                    
            }
        }
        int frag=0;
        for(l=0;l<b;l++){
            cin>>c;
            if(c){
                long long d,e,f;
                cin>>d>>e>>f;
                if(island[d-1][e-1]>f){
                    island[d-1][e-1]=f;
                    island[e-1][d-1]=f;
                    frag=1;
                }
            }
            else{
                
                if(frag){
                    frag=0;
                    for(k=0;k<a;k++){
                        for(j=0;j<a;j++){
                            for(i=0;i<a;i++)
                            island[i][j]=min(island[i][j],island[i][k]+island[k][j]);
                        }
                    }
                }
                long long d,e;
                cin>>d>>e;
                long long ans=island[d-1][e-1];
                if(ans<LLONG_MAX/2)
                cout<<ans<<endl;
                else
                cout<<"-1"<<endl;
            }
        }
    }
    return 0;
    
}

