### constant

MAX_INT = 1 << 30

### main

loop do
  n, k = gets.strip.split(' ').map{|s| s.to_i}
  break if (n | k) == 0

  edges = n.times.map{[]}
  nbrs = n.times.map{[]}

  k.times do
    qid, a, b, e = gets.strip.split(' ').map{|s| s.to_i}
    a -= 1
    b -= 1

    if qid == 1
      e0 = edges[a][b]
      if e0.nil?
        edges[a][b] = edges[b][a] = e
        nbrs[a] << b
        nbrs[b] << a
      elsif e0 > e
        edges[a][b] = edges[b][a] = e
      end
    else
      dists = n.times.map{MAX_INT}
      dists[a] = 0

      q = (0...n).to_a

      while ! q.empty?
        u = q.min{|a, b| dists[a] <=> dists[b]}
        q.delete u

        next if u == b

        for v in nbrs[u]
          du = dists[u] + edges[u][v]
          if dists[v] > du
            dists[v] = du
          end
        end
      end

      puts (dists[b] >= MAX_INT ? -1 : dists[b])
    end
  end
end