#include <bits/stdc++.h>
using namespace std;
#define Rep(i,N) for(int i = 0;i < N;i++)
#define F first
#define S second
typedef pair<int,int> Pi;
#define INF 1 << 29
#define pb push_back

inline void chmin(int &a, int b) {
  a = min(a,b);
}

inline void chmax(int &a, int b) {
  a = max(a,b);
}

int main()
{
  int N, K;
  while(cin >> N >> K,N || K) {
    int Q, A, B, C;
    int path[105][105];
    fill(path[0],path[105],INF);
    Rep(i,105) {
      path[i][i] = 0;
    }
    while(K--) {
      cin >> Q >> A >> B;--A,--B;
      if(Q) {
	cin >> C;
	chmin(path[A][B],C);
	chmin(path[B][A],C);
        Rep(i,N)Rep(j,N) {
	  chmin(path[i][j],min(path[i][A] + path[A][j],path[i][B] + path[B][j]));
	  path[j][i] = path[i][j];
	}
      }else {
	if(path[A][B] == INF) {
	  cout << -1 << endl;
	} else {
	  cout << path[A][B] << endl;
	}	
      }
    }
  }
  return 0;
}