#include<iostream>

using namespace std;

const int inf=1000000001;
int wf[100][100];

int main()
{
	for(int n,k;cin>>n>>k,n;){
		for(int i=0;i<n;i++)for(int j=0;j<n;j++)wf[i][j]=(i==j)?0:inf;

		while(k--){
			int type;	cin>>type;
			if(type==0){
				int a,b;	cin>>a>>b;
				a--,b--;
				cout<<(wf[a][b]<inf?wf[a][b]:-1)<<endl;
			}
			else{
				int c,d,e;	cin>>c>>d>>e;
				c--,d--;
				wf[c][d]=wf[d][c]=min(wf[c][d],e);
				for(int i=0;i<n;i++)for(int j=0;j<n;j++){
					wf[i][j]=min(wf[i][j],wf[i][c]+wf[c][d]+wf[d][j]);
					wf[i][j]=min(wf[i][j],wf[i][d]+wf[d][c]+wf[c][j]);
				}
			}
		}
	}

	return 0;
}