#include <iostream>
#include <algorithm>
using namespace std;
#define rep(x,to) for(int x=0; x<(to); ++(x))
#define INF 1<<29
int main() {
    int n, k, q, a, b, c;
    int dist[100][100] = {};
    while (cin >> n >> k, n || k) {
        rep(i,n) {
            rep(j,n) {
                if(i!=j) dist[i][j] = INF;
            }
        }

        rep(m,k) {
            cin >> q;
            if(q==0) {
                cin >> a >> b;
                a--, b--;
                int ans = dist[a][b] == INF ? -1 : dist[a][b];
                cout << ans << endl;
            } else {
                cin >> a >> b >> c;
                a--, b--;
                dist[a][b] = dist[b][a] = min(dist[a][b], c);
                rep(i,n) {
                    rep(j,n) {
                        dist[i][j] = min(dist[i][j], dist[i][a]+dist[a][b]+dist[b][j]);
                        dist[i][j] = min(dist[i][j], dist[i][b]+dist[b][a]+dist[a][j]);
                    }
                }
            }
        }
    }
    return 0;
}