#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll n, k, com, a, b, c, d[100];
vector<P> v[100];
ll INF = 1e15;

ll dijkstra(ll x, ll y) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (ll i = 0; i < n; i++) d[i] = INF;
  que.push(P(0, x));
  while (!que.empty()) {
    P p = que.top(); que.pop();
    ll from = p.second, cost = p.first;
    if (from == y) return cost;
    if (d[from] <= cost) continue;
    d[from] = cost;
    for (ll i = 0; i < v[from].size(); i++) {
      P q = v[from][i];
      ll to = q.second, c2 = cost + q.first;
      if (d[to] > c2) que.push(P(c2, to));
    }
  }
  return -1;
}

int main() {
  while (scanf("%lld%lld", &n, &k), n) {
    for (ll i = 0; i < k; i++) {
      scanf("%lld", &com);
      if (com == 0) {
        scanf("%lld%lld", &a, &b); a--; b--;
        printf("%lld\n", dijkstra(a, b));
      } else {
        scanf("%lld%lld%lld", &a, &b, &c); a--; b--;
        v[a].push_back(P(c, b));
        v[b].push_back(P(c, a));
      }
    }
    for (ll i = 0; i < n; i++) v[i].clear();
  }
}

