#include <bits/stdc++.h>
using namespace std ;
#define ll long long
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fir first
#define sec second
#define piint pair < int , int >
#define FOR( i , a , b ) for (int i = (a) , _b = (b) ; i <= _b ; i ++ )
#define pb push_back
#define str string
#define ALL(a) (a).begin() , (a).end()
#define rep( i , a , b) for (int i = (a) ; i < (b) ; i ++ )
#define ld long double
const int maxn = 1e3;
#define debug 0
// #define oo (ll)(1e18)
#define oo (ll)(1e15)
int n , k ;
struct CRUISE
{
bool kind ;
int u , v ;
ll cost ;
};
CRUISE a[5*maxn+3];
ll d[maxn+3][maxn+3] ;
void input(){
cin >> n >> k ;
FOR( i , 1 , n )
FOR( j , 1 ,n ){
if ( i == j ) d[i][j] = 0 ;
else d[i][j]=oo ;
}
FOR ( i , 1 , k ){
cin >> a[i].kind >> a[i].u >> a[i].v ;
if (a[i].kind) { cin >> a[i].cost ; }
}
}
void solve(){
FOR (i , 1 , k ){
auto c = a[i] ;
if (!a[i].kind){
if (debug) cout << c.u << ' ' << c.v << '\n';
if(d[c.u][c.v] == oo ) cout << -1 << '\n';
else cout << d[c.u][c.v] << '\n';
}
else{
d[a[i].u][a[i].v] = min ( a[i].cost , d[a[i].v][a[i].u] ) ;
d[a[i].v][a[i].u] = min ( a[i].cost , d[a[i].u][a[i].v] ) ;
FOR ( j , 1 , n ){
FOR ( k , 1 , n ){
if (d[j][c.u] != oo && d[c.v][k] != oo){
d[j][k] = min ( d[j][k] , d[j][c.u] + d[c.v][k] + d[c.u][c.v]) ;
}
}
}
}
}
}
#define name "CRUISE"
int main(){
fast
if(fopen(name".INP","r")) {
freopen (name".INP","r",stdin);
freopen (name".OUT","w",stdout);
}
input() ;
solve() ;
cerr << "\nTIME: = " << (1.0*clock())/CLOCKS_PER_SEC << '\n';
return(0) ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQgOyAKCgojZGVmaW5lIGxsIGxvb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EgNTAKMSAzMSAzNSA1NQoxIDM1IDM5IDYwCjEgMzkgNDMgNjUKMSA0MyA0NyA3MAoxIDQ3IDUwIDc1CjAgMSA1MAowIDMgNwowIDcgMTEKMCAxMSAxNQowIDE1IDE5CjAgMTkgMjMKMCAyMyAyNwowIDI3IDMxCjAgMzEgMzUKMCAzNSAzOQowIDM5IDQzCjAgNDMgNDcKMCA0NyA1MAoKCjEgMTEgMTIgNjAKMSAxMiAxMyA2NQoxIDEzIDE0IDcwCjEgMTQgMTUgNzUKMSAxNSAxNiA4MAoxIDE2IDE3IDg1CjEgMTcgMTggOTAKMSAxOCAxOSA5NQoxIDE5IDIwIDEwMAoxIDIwIDIxIDEwNQoxIDIxIDIyIDExMAoxIDIyIDIzIDExNQoxIDIzIDI0IDEyMAoxIDI0IDI1IDEyNQoxIDI1IDI2IDEzMAoxIDI2IDI3IDEzNQoxIDI3IDI4IDE0MAoxIDI4IDI5IDE0NQoxIDI5IDMwIDE1MAoxIDMwIDMxIDE1NQ==
50 200
1 1 2 10
1 2 3 20
1 3 4 15
1 4 5 25
1 5 6 30
1 6 7 35
1 7 8 40
1 8 9 45
1 9 10 50
1 10 11 55
1 11 12 60
1 12 13 65
1 13 14 70
1 14 15 75
1 15 16 80
1 16 17 85
1 17 18 90
1 18 19 95
1 19 20 100
1 20 21 105
1 21 22 110
1 22 23 115
1 23 24 120
1 24 25 125
1 25 26 130
1 26 27 135
1 27 28 140
1 28 29 145
1 29 30 150
1 30 31 155
1 31 32 160
1 32 33 165
1 33 34 170
1 34 35 175
1 35 36 180
1 36 37 185
1 37 38 190
1 38 39 195
1 39 40 200
1 40 41 205
1 41 42 210
1 42 43 215
1 43 44 220
1 44 45 225
1 45 46 230
1 46 47 235
1 47 48 240
1 48 49 245
1 49 50 250
0 1 10
0 1 20
0 1 30
0 1 40
0 1 50
0 10 20
0 20 30
0 30 40
0 40 50
1 1 25 100
1 25 50 110
0 1 25
0 1 50
1 10 25 50
0 1 25
1 25 30 20
0 1 30
1 30 35 10
0 1 35
1 35 40 15
0 1 40
1 40 45 5
0 1 45
1 45 50 5
0 1 50
1 5 15 50
1 15 25 60
1 25 35 70
1 35 45 80
1 45 50 90
0 5 15
0 15 25
0 25 35
0 35 45
0 45 50
1 2 10 100
1 10 20 200
1 20 30 300
1 30 40 400
1 40 50 500
0 2 10
0 10 20
0 20 30
0 30 40
0 40 50
1 1 50 50
0 1 50
1 1 10 5
0 1 10
1 10 20 5
0 1 20
1 20 30 5
0 1 30
1 30 40 5
0 1 40
1 40 50 5
0 1 50
0 1 10
0 1 20
0 1 30
0 1 40
0 1 50
0 5 10
0 10 15
0 15 20
0 20 25
0 25 30
0 30 35
0 35 40
0 40 45
0 45 50
1 1 5 7
1 5 10 8
1 10 15 9
1 15 20 10
1 20 25 11
1 25 30 12
1 30 35 13
1 35 40 14
1 40 45 15
1 45 50 16
0 1 5
0 5 10
0 10 15
0 15 20
0 20 25
0 25 30
0 30 35
0 35 40
0 40 45
0 45 50
1 1 50 100
0 1 50
1 1 20 80
1 20 40 90
1 40 50 70
0 1 20
0 20 40
0 40 50
1 5 25 50
1 25 45 60
1 45 50 70
0 5 25
0 25 45
0 45 50
1 1 3 15
1 3 7 20
1 7 11 25
1 11 15 30
1 15 19 35
1 19 23 40
1 23 27 45
1 27 31 50
1 31 35 55
1 35 39 60
1 39 43 65
1 43 47 70
1 47 50 75
0 1 50
0 3 7
0 7 11
0 11 15
0 15 19
0 19 23
0 23 27
0 27 31
0 31 35
0 35 39
0 39 43
0 43 47
0 47 50
1 11 12 60
1 12 13 65
1 13 14 70
1 14 15 75
1 15 16 80
1 16 17 85
1 17 18 90
1 18 19 95
1 19 20 100
1 20 21 105
1 21 22 110
1 22 23 115
1 23 24 120
1 24 25 125
1 25 26 130
1 26 27 135
1 27 28 140
1 28 29 145
1 29 30 150
1 30 31 155