#include<bits/stdc++.h>
using namespace std;
int n, m;
int via[2505][2505] = { 0 };
int f[10005] = { 0 };
int vis[10005] = { 0 };
int cnt = 0;
struct node {
int to,next;
int z;
}edge[1000005];
void add(int x, int y, int z) {
edge[++cnt].to = y;
edge[cnt].next = f[x];
edge[cnt].z = z;
f[x] = cnt;
}
void dj(int x) {
queue<int> q;
q.push(x);
fill(vis, vis + n + 1, 0);
while (!q.empty()) {
int u, v;
u = q.front();
q.pop();
if (vis[u]) continue;
vis[u] = 1;
for (int i = f[u]; i; i = edge[i].next) {
v = edge[i].to;
if (via[x][v] > via[x][u] + edge[i].z || via[x][v] == 0) {
via[x][v] = via[x][u] + edge[i].z;
q.push(v);
}
}
}
}
int main() {
cin >> n >> m;
int i, j;
for (i = 1; i <= m; i++) {
int op;
int x, y, z;
cin >> op;
if (op) {
cin >> x>> y>> z;
add(x, y, z);
add(y, x, z);
}
else {
cin >> x >> y;
dj(x);
if (via[x][y] == 0) {
cout << -1 << endl;
}
else
cout << via[x][y] << endl;
}
}
return 0;
}