#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
using namespace std;

const int MAX = 1 << 20;

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  for(int n,k;cin>>n>>k,n;){
    vector<vector<int>> fare(n,vector<int> (n,MAX));
    for(int i=0;i<n;++i)
      fare[i][i]=0;

    for(int i=0;i<k;++i){
      int y;cin>>y;
      if(y==1){
	int c,d,e;cin>>c>>d>>e;--c;--d;

	fare[c][d]=min(fare[c][d],e);
	fare[d][c]=min(fare[d][c],e);

	// wa-sharu floydo hou
	for(int o=0;o<n;++o)
	  for(int p=0;p<n;++p){
      	    fare[o][p] = min(fare[o][p],fare[o][c] + fare[c][d] + fare[d][p]);
	    fare[o][p] = min(fare[o][p],fare[o][d] + fare[d][c] + fare[c][p]);
	  }
      }else{
	int a,b;cin>>a>>b;--a;--b;

	if(fare[a][b]==MAX)
	  cout<<-1<<endl;
	else
	  cout<<fare[a][b]<<endl;
      }
    }
  }

  return EXIT_SUCCESS;
}