//AOJ 0200
//菴ソ縺?婿繧ゅｏ縺九ｉ縺ェ縺?¥縺帙↓include縺励∪縺上ｋ莠コ髢薙?繧ッ繧コ
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<list>
#include<deque>
using namespace std;
#define INF 1 << 20
#define MAX_B 101

int a, b, i, j, k, money[MAX_B][MAX_B], time[MAX_B][MAX_B], n, m, c, d;
bool judge;
stack<int> left, right;


int main(){
  scanf("%d %d", &n, &m);
  if(n == 0 && m == 0) return 0;
  for(i = 1; i <= n; i++){
    for(j = 1; j <= n; j++){
      money[i][j] = INF;
    }
  }
  for(i = 1; i <= n; i++){
    money[i][i] = 0;
  }
  for(i = 0; i < m; i++){
    scanf("%d", &a);
    if(a == 0){
      scanf("%d %d", &b, &c);
      if(money[b][c] == INF) printf("-1\n");
      else printf("%d\n", money[b][c]);
      continue;
    }
    scanf("%d %d %d", &b, &c, &d);
    if(money[b][c] > d){
      money[b][c] = d;
      money[c][b] = d;
      for(j = 1; j <= n; j++){
	money[j][c] = money[c][j] = min(money[j][c], money[j][b] + money[b][c]);
	money[j][b] = money[b][j] = min(money[j][b], money[j][c] + money[c][b]);
      }
    }
  }
  return 0;
}