
// code by lynmisakura. wish to be accepted!

#include<bits/stdc++.h>
using namespace std;

#define rep(i,N) for(int i = 0;i < N;i++)
#define rng(i,a,b) for(int i = a;i < b;i++)

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

#define lmd(func) auto func = [&]

#define mp make_pair
#define pb push_back
#define eb emplace_back

#define debug(arr) cout << #arr << " = " << arr << '\n'
#define debug2(a,b) cout<<"["<<#a<<","<<#b<<"] = "<<"["<<a<<","<<b<<"]\n"
#define debug3(a,b,c) cout<<"["<<#a<<","<<#b<<","<<#c<<"] = "<<"["<<a<<","<<b<<","<<c<<"]\n"
template<class T> ostream &operator << (ostream& out, const vector<T>& arr) {
  cout << "{"; for (int i = 0; i < arr.size(); i++)cout << (!i ? "" : ", ") << arr[i]; cout << "}";
  return out;
}
template<class T> ostream &operator << (ostream& out, const vector<vector<T> >& arr) {
  cout << "{\n"; for (auto& vec : arr)cout << "  " << vec << ",\n"; cout << "}";
  return out;
}
template<class S,class T> ostream &operator << (ostream& out, const pair<S,T>& p){
  cout << "{" << p.first << "," << p.second << "}" << '\n';
  return out;
}
template<class T> istream &operator >> (istream& in, vector<T>& arr) {
  for (auto& i : arr)cin >> i; return in;
}
template<class S,class T> istream &operator >> (istream& in,pair<S,T>& p){
  cin >> p.first >> p.second; return in;
}

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;

/////////////////////////////////////////////////////////////////////////////

template<class T> void chmin(T& a,T b){ a = min(a,b);}
template<class T> void chmax(T& a,T b){ a = max(a,b);}

int n;
ll mx[200020],mn[200020];

ll dp[200020][2];

int main(void){
  cin.tie(0);
  ios::sync_with_stdio(false);
  //cout << fixed << setprecision(20);

  cin >> n;
  rep(i,n+1){
    mx[i] = -INT_MAX;
    mn[i] = INT_MAX;
  }
  
  vector<int> cs;
  vector<pair<ll,ll>> a(n);
  rep(i,n){
    ll x,c;cin >> x >> c;
    c--;
    a[i] = mp(x,c);
    cs.pb(c);
  } 
  
  sort(all(cs));
  cs.erase(unique(cs.begin(),cs.end()),cs.end());

  rep(i,n){
    auto[x,c] = a[i];
    c = lower_bound(all(cs),c) - cs.begin();
    chmax(mx[c],x);
    chmin(mn[c],x);
  }

  rep(i,200020)rep(j,2) dp[i][j] = (1LL << 55);

  int i = 0;
  
  dp[i][0] = abs(mx[i]) + mx[i] - mn[i];
  dp[i][1] = abs(mn[i]) + mx[i] - mn[i];

//  debug2(mn[i],mx[i]);
  
  i++;
  n = cs.size();
  for(;i < n;i++){
    
    ll l = mn[i-1],r = mx[i-1];
    ll nl = mn[i],nr = mx[i];  
    
    chmin(dp[i][0] , dp[i-1][0] + abs(l - nr) + nr - nl);
    chmin(dp[i][0] , dp[i-1][1] + abs(r - nr) + nr - nl);
    chmin(dp[i][1] , dp[i-1][0] + abs(l - nl) + nr - nl);
    chmin(dp[i][1] , dp[i-1][1] + abs(r - nl) + nr - nl);
  }
  
  cout << min(dp[n-1][0] + abs(mn[n-1]),dp[n-1][1] + abs(mx[n-1])) << '\n';

}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long n,x,a[110],f[110][110][110],ans=1145141919810233454;
int main()
{
	scanf("%lld %lld",&n,&x);
	for(int i=1;i<=n;++i)	scanf("%lld",&a[i]);
	for(int s=1;s<=n;++s)
	{
		memset(f,-0x3f,sizeof(f));
		f[0][0][0]=0;
		for(int i=1;i<=n;++i)
		{
			for(int j=1;j<=min(s,i);++j)
			{
				for(int k=0;k<n;++k)	f[i][j][k]=max(f[i-1][j][k],f[i-1][j-1][((k-a[i])%s+s)%s]+a[i]);
			}
		}
		if(f[n][s][x%s]>=0)	ans=min(ans,(x-f[n][s][x%s])/s);
	}
	printf("%lld\n",ans);
	return 0;
}