#include <bits/stdc++.h>
using namespace std;

#define INF 0x3f3f3f3f

int dp[100][100];

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

  while(true) {
  int n, k;
  cin >> n >> k;
  if(n==0&&k==0) break;
  memset(dp, 0x3f, sizeof(dp));
  for (int i = 0; i < n; i++) {
    dp[i][i] = 0;
  }
  for (int i = 0; i < k; i++) {
    int a, b, c, d;
    cin >> a >> b >> c; b--; c--;
    if (!a) {
      if (dp[b][c] == INF) cout << "-1" << endl;
      else cout << dp[b][c] << endl;
      continue;
    }
    cin >> d;
    if (dp[b][c] <= d) continue;
    dp[b][c] = dp[c][b] = d;
    for (int j = 0; j < n; j++) {
      for (int x = j+1; x < n; x++) {
        dp[j][x] = dp[x][j] = min(dp[j][x], min(dp[j][b] + dp[c][x], dp[j][c] + dp[b][x]) + d);
      }
    }
  }
  }

	return 0;
}

