// AOJ 0526
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#define rep(i,a) for( int i = 0; i != (a); ++i )

const int INF = 1 << 30;
typedef std::pair<int, int> P;

int n, k;
std::vector<P> G[100];
int d[100];

void dijkstra( int s )
{
	std::fill( d, d+n, INF );
	std::priority_queue<P> pque;
	pque.push( P( 0, s ) );
	d[s] = 0;

	while( !pque.empty() )
	{
		P p = pque.top(); pque.pop();
		int dist = -p.first, v = p.second;

		if( d[v] < dist )
			continue;

		rep( i, G[v].size() )
		{
			P &e = G[v][i];

			if( d[e.first] > d[v]+e.second )
			{
				d[e.first] = d[v]+e.second;
				pque.push( P( -d[e.first], e.first ) );
			}
		}
	}

	return;
}

int main()
{
	while( scanf( "%d%d", &n, &k ), n|k )
	{
		rep( i, 100 )
			G[i].clear();

		rep( i, k )
		{
			int t;
			scanf( "%d", &t );

			if( t )
			{
				int c, d, e;
				scanf( "%d%d%d", &c, &d, &e );
				--c; --d;

				G[c].push_back( P( d, e ) );
				G[d].push_back( P( c, e ) );
			}
			else
			{
				int a, b;
				scanf( "%d%d", &a, &b );
				--a; --b;

				dijkstra( a );

				printf( "%d\n", d[b]==INF?-1:d[b] );
			}
		}
	}

	return 0;
}