n=int(input()) l=[eval(input().replace(' ',"+4*")) for _ in range(n)] m=max(l) a=0 for x in l: if (m-x)%2: a=-1; break a+=(m-x)//2 print(a)