#include #include using namespace std; int gcd(int x,int y){ if((x == 0) || (y == 0)) return 0; while(x != y){ if(x > y) x -= y; else y -= x; } return x; } int lcm(int x,int y){ if((x == 0) || (y == 0)) return 0; return (x * y / gcd(x,y)); } int main(){ int ans = 0,lenmax = 0,n; bool oe; cin >> n; int a[n],b[n],length[n]; for(int i = 0;i < n;i++){ cin >> a[i] >> b[i]; length[i] = a[i] + 4 * b[i]; if(!i) oe = length[i] % 2; if(oe != length[i] % 2){ cout << -1 << endl; return 0; } lenmax = max(lenmax,lcm(lenmax,length[i])); } for(int i = 0;i < n;i++) ans += lenmax - length[i]; cout << ans / 2 << endl; }