#include <algorithm>
// #include <atcoder/all>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <vector>
/*
cd $dir && g++ -std=c++17 -Wall -Wextra -O2 -DATCODERDEBUG
-I/mnt/d/MyProjects/atcoder/lib/ac-library $fileName && echo 'compilation ok!'
&&  ./a.out
*/
using namespace std;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define ALL(x) x.begin(), x.end()
#define dame(a)        \
  {                    \
    cout << a << endl; \
    return 0;          \
  }
typedef long long ll;

class dstream : public ostream {};

template <typename T>
dstream &operator<<(dstream &os, T v) {
#ifdef ATCODERDEBUG
  cout << "\033[1;31m" << v << "\033[0m";
#endif
  return os;
}

ostream &endd(std::ostream &out) {
#ifdef ATCODERDEBUG
  out << std::endl;
#endif
  return out;
}
dstream dout;

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  for (auto const &x : v) {
    os << x;
    if (&x != &v.back()) {
      os << " : ";
    }
  }
  os << " ]" << endl;
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  os << "[" << endl;
  for (auto const &x : v) {
    os << " ";
    os << x;
  }
  os << "]" << endl;
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
  os << "[ " << v.first << " " << v.second << " ]" << endl;
  return os;
}

template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x)) {
    xmax = x;
    return true;
  }
  return false;
}

template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin)) {
    xmin = x;
    return true;
  }
  return false;
}

ll op(ll a, ll b) { return min(a, b); }

ll e() { return 1e17; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll N, x, c;
  cin >> N;
  vector<vector<ll>> c2xs(N, vector<ll>());
  REP(i, N) {
    cin >> x;
    cin >> c;
    c2xs[c - 1].emplace_back(x);
  }

  REP(i, N) { sort(c2xs[i].begin(), c2xs[i].end()); }

  ll left = 0;
  ll right = 0;
  ll left_v = 0;
  ll right_v = 0;

  REP(c, N) {
    ll siz = c2xs[c].size();
    if (siz == 0) continue;

    ll minX = c2xs[c][0];
    ll maxX = c2xs[c][siz - 1];

    ll n_right_v = min(left_v + abs(left - minX), right_v + abs(right - minX)) +
                   (maxX - minX);
    ll n_left_v = min(right_v + abs(right - maxX), left_v + abs(left - maxX)) +
                  (maxX - minX);

    right_v = n_right_v;
    left_v = n_left_v;
    right = maxX;
    left = minX;

    dout << left << " " << left_v << " " << right << " " << right_v << endd;
  }

  ll ans = 1e17;
  chmin(ans, left_v + abs(left));
  chmin(ans, right_v + abs(right));

  cout << ans << endl;
  // cout << std::fixed << std::setprecision(15) << sqrt(ans) << endl;
}
#pragma GCC optimize("Ofast")
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <cmath>
#include <iomanip>
#include <string>
#include <deque>
#include <assert.h>
#include <random>
#include <ctime>
#include <unordered_map>
#include <complex>
#include <chrono>

#define pnode node*

using namespace std;

typedef long long ll;
typedef long double ld;

const ll mod = (ll)1e9 + 7;

ll pw(ll a, ll p) {
    if (p == 0) {
        return 1;
    }
	ll r1 = pw(a, p / 2);
    if (p % 2 == 0) {
		return (r1 * r1) % mod;
    } else {
        return (pw(a, p - 1) * a) % mod;
    }
}

ll rev(ll x) {
    return pw(x, mod - 2);
}

ll f[(ll)2e7];


ll c(ll n, ll k) {
    if (k > n) {
		return 0;
    }
    ll ans = 1;
//	cerr << n << " " << k << "\n";
    for (ll i = n; i >= n - k + 1; --i) {
        ans *= i;
        ans %= mod;
    }
//    cerr << ans << "\n";
    ans = (ans * rev(f[k])) % mod;
    return ans;
}


void solve(){
	f[0] = 1;
    for (ll i = 1; i < 2e7; ++i) {
        f[i] = (f[i - 1] * i) % mod;
    }
    ll n, m;
    vector < ll > a;
    cin >> n >> m;
    a.resize(n);
    ll sm = 0;
    for (ll i = 0; i < n; ++i) {
		cin >> a[i];
		sm += a[i];
    }
    sm += n;
    m += n;
    cout << c(m, sm) << "\n";
}

signed main(){
    ll t = 1;
    ios_base::sync_with_stdio(0);
    srand(time(0));
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout << setprecision(20);
    #ifdef DEBUG
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #else
    #endif
    while(t--){
        solve();
    }
}
