#include <iostream>
using namespace std;
int s[20], b[20], f[20];
int n, j, c = 1, y, ans = 2e9;
void dfs(int x){
if(x < n)
for(int i = x; i <= n; i++)
if(!f[i]){
c *= s[i];
y += b[i];
ans = min(ans, abs(c - y));
f[i] = 1;
dfs(x + 1);
f[i] = 0;
c /= s[i];
y -= b[i];
}
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++)
cin >> s[i] >> b[i];
dfs(1);
cout << ans << endl;
return 0;
}