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

using ll = long long int;
using P = pair<int, int>;
using P3 = pair<int,P>;
using PP = pair<P, P>;
constexpr int INF = 1<<29;

bool chmin(int &a, int b){
    if(a > b){
        a = b;
        return true;
    }
    return false;
}

bool solve(){
    int n, m;
    cin >> n >> m;
    if(n == 0) return false;
    vector<vector<int> > d(n, vector<int>(n,INF));
    for(int i=0;i<n;i++){
        d[i][i] = 0;
    }
    for(;m>0;m--){
        int x, a, b;
        cin >> x >> a >> b;
        a--; b--;
        if(x == 0){
            cout << (d[a][b] < INF ? d[a][b] : -1) << endl;
        }else{
            int c;
            cin >> c;
            if(chmin(d[a][b], c)){
                d[b][a] = c;
                for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        chmin(d[i][j], d[i][a]+d[a][b]+d[b][j]);
                        chmin(d[i][j], d[i][b]+d[b][a]+d[a][j]);
                    }
                }
            }
        }
    }
    return true;
}

int main(){
    while(solve());
    return 0;
}

