Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0526

"""
import sys
from sys import stdin
from heapq import heappush, heappop
input = stdin.readline


def warshall_floyd(d, V):
    for k in range(V):
        for i in range(V):
            for j in range(V):
                new_cost = d[i][k] + d[k][j]
                if new_cost < d[i][j]:
                    d[i][j] = new_cost


def main(args):
    while True:
        n, k = map(int, input().split())
        if n == 0 and k == 0:
            break

        d = [[float('inf')] * (n+1) for _ in range(n+1)]
        #for i in range(n+1):
        #    d[i][i] = 0

        updated = False
        for _ in range(k):
            data = [int(x) for x in input().split()]
            if data[0] == 0:
                if updated:
                    warshall_floyd(d, n+1)
                    updated = False
                f, t = data[1], data[2]
                if d[f][t] == float('inf'):
                    print(-1)
                else:
                    print(d[f][t])
            else:
                f, t, c = data[1], data[2], data[3]
                if c < d[f][t]:
                    d[f][t] = c
                    updated = True
                if c < d[t][f]:
                    d[t][f] = c
                    updated = True


if __name__ == '__main__':
    main(sys.argv[1:])
    

Fixed submission:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0526

"""
import sys
from sys import stdin
from heapq import heappush, heappop
input = stdin.readline


def dijkstra(s, G):
    pq = []
    d = [float('inf')] * len(G)
    d[s] = 0
    heappush(pq, (0, s))

    while pq:
        c, v = heappop(pq)
        if d[v] < c:
            continue
        for t, c in G[v].items():          #  ?????????v?????£??\??????????????±????????¨?????????????????°?????§?????????????????´??°
            if d[t] > d[v] + c: #  ???????????§??\??£?????????????????????????????????v????????±????????????????????????????????????????????°???????????????????????´??°
                d[t] = d[v] + c
                heappush(pq, (d[t], t)) #  python???heapq??????????°???????????????????pop?????????????????????????????§???-1??????????????????
    return d


def main(args):
    while True:
        n, k = map(int, input().split())
        if n == 0 and k == 0:
            break

        G = [{} for _ in range(n+1)]
        costs = [[float('inf')]*(n+1) for _ in range(n+1)]

        for _ in range(k):
            data = [int(x) for x in input().split()]
            if data[0] == 0:
                f, t = data[1], data[2]
                costs[f] = dijkstra(f, G)
                if costs[f][t] == float('inf'):
                    print(-1)
                else:
                    print(costs[f][t])
            else:
                f, t, c = data[1], data[2], data[3]
                if t in G[f]:
                    G[f][t] = min(G[f][t], c)
                else:
                    G[f][t] = c
                if f in G[t]:
                    G[t][f] = min(G[t][f], c)
                else:
                    G[t][f] = c


if __name__ == '__main__':
    main(sys.argv[1:])