### subroutines

def rootd(k)
  rk = k
  rd = 0
  while $uts[rk] != rk
    rd += $dfs[rk]
    rk = $uts[rk]
  end
  $uts[k] = rk
  $dfs[k] = rd
  [rk, rd]
end

### main

loop do
  n, m = gets.split.map(&:to_i)
  break if (n | m) == 0

  $uts = Array.new(n + 1){|i| i}
  $dfs = Array.new(n + 1){0}

  m.times do
    cols = gets.strip.split
    
    op, a, b = cols
    a = a.to_i
    b = b.to_i
    ra, da = rootd(a)
    rb, db = rootd(b)
    #p ["rootd", a, ra, da]
    #p ["rootd", b, rb, db]
    
    if op == '!'
      if ra == rb
        next if w == da - db
        puts "Error"
        exit
      end

      w = cols[3].to_i
      rd = w + da - db
      if rd >= 0
        $uts[ra] = rb
        $dfs[ra] = rd
      else
        $uts[rb] = ra
        $dfs[rb] = -rd
      end
    else
      if ra != rb
        puts "UNKNOWN"
      else
        d = da - db
        puts d
      end
    end

    #p cols
    #p $uts
    #p $dfs
  end
end