#include <bits/stdc++.h>

using namespace std;

typedef long long  ll;

typedef ll Weight;
const Weight INF = 1e9;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

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

    int n, k, flag, x, y, c;

    while (cin >> n >> k, n || k) {
        Matrix d(n, Array(n, INF));

        for (int i = 0; i < n; ++i)
            d[i][i] = 0;

        for (int i = 0; i < k; ++i) {
            cin >> flag;
            if (flag == 0) { // ??¢
                cin >> x >> y;
                --x, --y;
                cout << (d[x][y] >= INF ? -1 : d[x][y]) << '\n';
            }
            else if (flag == 1) { // ??????
                cin >> x >> y >> c;
                --x, --y;
                for (int j = 0; j < n; ++j)
                    for (int k = j + 1; k < n; ++k)
                        d[k][j] = d[j][k] = min(d[j][k], min(d[j][x] + d[y][k] + c,
                                                             d[j][y] + d[x][k] + c));
            }
        }
    }
    return 0;
}