#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O0")
typedef long long ll;
typedef long double ld;
const ll mod = 1e9+7;
const ll INF = 1e18;
#define rep(i,n) for (ll i = 0; i < (n); ++i)
#define Rep(i,a,n) for (ll i = (a); i < (n); ++i)
#define All(a) (a).begin(),(a).end()
#define Pi acos(-1)
using V = vector<ll>;
using P = pair<ll,ll>;
using Graph = vector<V>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << setprecision(15) << fixed;

  ll s, p;
  cin >> s >> p;
  for (ll i = 1; i <= sqrt(p) + 100; ++i) {
    if (p%i == 0) {
      ll j = p/i;
      if (i+j == s) {
        cout << "Yes\n";
        return 0;
      }
    }
  }
  cout << "No\n";
}
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define eb emplace_back
#define Field(T) vector<vector<T>>
#define pq(T) priority_queue<T,vector<T>,greater<T>>
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main() {
  int n;
  cin >> n;
  vector<vector<ll>> cnt(n+1);
  cnt[n].eb(0);
  set<int> st;
  st.emplace(n);
  rep(i,0,n) {
    ll x;
    int c;
    cin >> x >> c;
    --c;
    cnt[c].eb(x);
    st.emplace(c);
  }
  ll a1 = 0, a2 = 0;
  ll ps = 0, pt = 0;
  for (auto e : st) {
    vector<ll> &v = cnt[e];
    sort(all(v));
    ll ns = v.front(), nt = v.back();
    ll na1 = min(a1 + abs(ps - nt), a2 + abs(pt - nt)) + nt - ns;
    ll na2 = min(a1 + abs(ps - ns), a2 + abs(pt - ns)) + nt - ns;
    a1 = na1, a2 = na2;
    ps = ns, pt = nt;
  }
  cout << min(a1, a2) << endl;
  return 0;
}