#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,n,lcmkeep; cin >> n; int a[n],b[n],space[n]; for(int i = 0;i < n;i++) cin >> a[i] >> b[i]; for(int i = 0;i < n;i++) space[i] = a[i] + 4 * b[i]; for(int i = 0;i < n;i++){ if(i == 0){ lcmkeep = lcm(space[i],space[i + 1]); i += 2; }else lcmkeep = lcm(lcmkeep,space[i]); } for(int i = 0;i < n;i++) ans += lcmkeep - space[i]; cout << ans << endl; }