import std.stdio, std.conv, std.string, std.range, std.math, std.algorithm; void main() { auto N = readln.strip.to!int; int[] a, b; foreach (_; 0 .. N) { auto input = readln.split.to!(int[]); a ~= input[0]; b ~= input[1]; } auto widths = zip(a, b).map!"a[0]+4*a[1]"; auto diffs = widths.map!(diff => widths.minPos!"a>b".front-diff); (diffs.filter!"a%2==1".empty ? diffs.sum / 2 : -1).writeln; }