import sys def main(): N = int(input()) spaces_list = [] odd = False even = False for i in range(N): space, tab = map(int, input().split()) spaces = space + 4 * tab if spaces % 2 == 0: even = True else: odd = True if odd and even: print(-1) sys.exit() spaces_list.append(spaces) #print(spaces_list) if len(set(spaces_list)) == 1: print(0) max_spaces = max(spaces_list) needed_spaces = 0 for spaces in spaces_list: needed_spaces += max_spaces - spaces print(int(needed_spaces / 2)) if __name__ == '__main__': main()