def main(): N = int(input()) spaces = [] max_space = 0 for _ in range(N): a, b = map(int, input().split()) space = a + 4 * b spaces.append(space) max_space = max(max_space, space) diffs = 0 for space in spaces: diff = max_space - space if diff % 2 != 0: print(-1) return diffs += diff print(diffs // 2) if __name__ == "__main__": main()