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

typedef pair<int, int> pii ;

struct edge
{
  int to, cost;
};

int main()
{
  int n, k;
  while (cin >> n >> k, n){
    int d[100];
    vector<edge> G[100];
    priority_queue<pii, vector<pii>, greater<pii> > q;

    for (int i = 0; i < k; i++){
      int a, b, c;
      cin >> a >> b >> c;
      b--; c--;

      if (a) {
        int e;
        cin >> e;

        edge E;
        E.to = c; E.cost = e;
        G[b].push_back(E);
        E.to = b;
        G[c].push_back(E);

      } else {
        fill(d, d + n, 1e9);
        d[b] = 0;
        q.push(make_pair(0, b));

        while (!q.empty()) {
          pii p = q.top(); q.pop();
          int v = p.second;
          if (d[v] < p.first) continue;
          for (int i = 0; i < G[v].size(); i++){
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
              d[e.to] = d[v] + e.cost;
              q.push(make_pair(d[e.to], e.to));
            }
          }
        }
        
        cout << (d[c] == 1e9 ? -1 : d[c]) << endl;
      }
    }
  }
}