#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll> pll;

const ll inf = 1LL<<60;

int main(){
    int n;  cin >> n;
    vector<pll> vec(n+1, make_pair(inf, -1*inf));
    rep(i, n)
    {
        ll x, c; cin >> x >> c;
        vec[c].first = min(vec[c].first, x);
        vec[c].second = max(vec[c].second, x);
    }
    vector<pll> dp(n+1, make_pair(0, 0));
    ll l_l = 0, l_r = 0;
    rep(i, n)
    {
        ll l = vec[i+1].first, r = vec[i+1].second;
        if(l == inf){
            dp[i+1].first = dp[i].first;
            dp[i+1].second = dp[i].second;
        }else{
            ll ff,fs,sf,ss;
            if(l_l < r)   ff = (r - l_l) + (r - l);
            else    ff = l_l - l;
            if(l_r < r)   sf = (r - l_r) + (r - l);
            else    sf = l_r - l;
            dp[i+1].first = min(dp[i].first + ff, dp[i].second + sf);
            if(l < l_l)   fs = (l_l - l) + (r - l);
            else    fs = r - l_l;
            if(l < l_r)   ss = (l_r-l) + (r-l);
            else    ss = r - l_r;
            dp[i+1].second = min(dp[i].first + fs, dp[i].second + ss);
            l_l = l;
            l_r = r;
            //cout << ff  << ':' << sf << ':' << fs << ':' << ss << endl;
            //cout << dp[i+1].first << ':' << dp[i+1].second << endl;
        }
    }
    ll ans = min(dp[n].first + abs(l_l), dp[n].second + abs(l_r));
    cout << ans << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int INF = 2e9;

vector<int> divisor(int x) {
	vector<int> ret;
	for (int i = 1; i * i <= x; i++) {
		if (x % i == 0) {
			ret.emplace_back(i);
			if (i * i != x) ret.emplace_back(x / i);
		}
	}
	return ret;
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int n;
	cin >> n;
	int mini = INF;
	unordered_map<int, int> mp;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		for (int d : divisor(x)) {
			mp[d] = __gcd(mp[d], x);
		}
		mini = min(mini, x);
	}
	int ans = 0;
	for (auto& e : mp) {
		if (e.first > mini) continue;
		ans += (e.first == e.second);
	}
	cout << ans << '\n';
 
    return 0;
}