def lscan; gets.split.map(&:to_i); end def bye(msg); puts msg.to_s; exit; end def 🎍(a,b,c) a != c && ((a < b && b > c) || (a > b && b < c)) end def solve1(a,b,c) # bmax cost = 0 if b <= c cost += c - b + 1 c = b - 1 end if b <= a cost += a - b + 1 a = b - 1 end if a == c cost += 1 a -= 1 end return a > 0 && b > 0 && c > 0 && cost end def solve2(a,b,c) # bmin cost = 0 if b >= c cost += b - c + 1 b = c - 1 end if b >= a cost += b - a + 1 b = a - 1 end if a == c if a-b > 1 a -= 1 cost += 1 else a -= 1 x = a < b ? 2 : 1 b -= x cost += 1 + x end end #p [a,b,c, cost] return a > 0 && b > 0 && c > 0 && cost end def query(a,b,c) return 0 if 🎍(a,b,c) a,c = c,a if a > c s1 = solve1(a,b,c) s2 = solve2(a,b,c) return s1 && s2 ? [s1, s2].min : s1 || s2 ? s1 || s2 : -1 end gets.to_i.times do p query(*lscan) end