#include <bits/stdc++.h>
using namespace std;
void init(int n, int cost[100][100])
{
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j)
cost[i][j] = -1;
cost[i][i] = 0;
}
}
int calc_cost(int cost[100][100], int a, int b)
{
--a; --b;
return cost[a][b];
}
void update(int n, int cost[100][100], int c, int d)
{
int cost_cd = cost[c][d];
for (int i = 0; i < n; ++i) {
int cost_ic = cost[i][c];
if (cost_ic == -1)
continue;
for (int j = 0; j < n; ++j) {
int cost_dj = cost[d][j];
if (cost_dj == -1)
continue;
int cost_ij = cost_ic + cost_cd + cost_dj;
if (cost[i][j] == -1 || cost[i][j] > cost_ij)
cost[i][j] = cost_ij;
}
}
}
void update(int n, int cost[100][100], int c, int d, int e)
{
--c; --d;
if (cost[c][d] != -1 && cost[c][d] <= e)
return;
cost[c][d] = cost[d][c] = e;
update(n, cost, c, d);
update(n, cost, d, c);
}
int main()
{
int n, k;
cin >> n >> k;
int cost[100][100];
init(n, cost);
for (int i = 0; i < k; ++i) {
int info, a, b, c, d, e;
cin >> info;
if (info == 0) {
cin >> a >> b;
cout << calc_cost(cost, a, b) << endl;
}
if (info == 1) {
cin >> c >> d >> e;
update(n, cost, c, d, e);
}
}
}