#include <iostream>
#include <functional>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <iomanip>
#include <complex>
#include <random>
#include <bitset>
#include <list>
// #include <prettyprint.hpp>
using namespace std;
#define repi(i,n) for(int i=0;i<n;++i)
#define rep(i,n) for(ll i=0;i<n;++i)
#define repinvi(i,n) for(int i=n-1;i>=0;--i)
#define sll(n) scanf("%lld", &n);
#define sii(n) scanf("%d", &n);
#define slf(n) scanf("%lf", &n);
#define pll pair<ll,ll>
#define pii pair<int,int>
#define psi pair<si,si>
#define v(t) vector<t>
#define vv(t) vector<vector<t>>
#define vvv(t) vector<vector<vector<t>>>
typedef long long ll;
typedef double lf;
typedef short int si;

static const ll INF = 1e18;
static const ll max_n = 200010;

v(v(ll)) dp(max_n, v(ll)(2, 0));
// v(ll) cs(max_n);
// v(ll) xs(max_n);

void Main(){
    ll N, X, C, x, c;
    // cout << dp << endl;
    cin >> N;
    map<ll, v(ll)> cs2x;
    v(ll) color_l;
    rep(i, N){
        cin >> x >> c;
        cs2x[c].emplace_back(x);
    }

    for(auto p: cs2x){
        sort(p.second.begin(), p.second.end());
        cs2x[p.first] = p.second;
        color_l.emplace_back(p.first);
    }

    // cout << cs2x <<endl;

    sort(color_l.begin(), color_l.end());
    // cout << "color_l:" << color_l << endl;

    for(ll i=0;i<color_l.size(); ++i){
        c = color_l[i];
        ll now_x0, now_x1;
        if(i==0){
            now_x0 = dp[i][0];
            now_x1 = dp[i][1];
        }else{
            ll pc = color_l[i-1];
            now_x0 = cs2x[pc][0];
            now_x1 = cs2x[pc][cs2x[pc].size()-1];
        }

        ll color_le = cs2x[c][0];
        ll color_re = cs2x[c][cs2x[c].size()-1];

        ll this_color_l = color_re - color_le;

        ll dist_from0 = abs(now_x0 - color_re) + this_color_l + dp[i][0];
        ll dist_from1 = abs(now_x1 - color_re) + this_color_l + dp[i][1];

        dp[i+1][0] = min(dist_from0, dist_from1);

        dist_from0 = abs(now_x0 - color_le) + this_color_l + dp[i][0];
        dist_from1 = abs(now_x1 - color_le) + this_color_l + dp[i][1];

        dp[i+1][1] = min(dist_from0, dist_from1);

        // cout << "color r,l=" << color_re << "," << color_le <<endl;
        // cout << "dp[" << i+1 << "]=" << dp[i+1] <<endl;
    }


    ll max_color = color_l[color_l.size()-1];
    ll max_color_i = color_l.size();
    ll ans = min(dp[max_color_i][0]+abs(cs2x[max_color][0]), dp[max_color_i][1]+abs(cs2x[max_color][cs2x[max_color].size()-1]));

    cout << ans <<endl;

}

int main(){
    Main();
}
#include <bits/stdc++.h>
using namespace std;

// template {{{
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

#define  range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)
#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1}; 
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};

struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; } 
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}

void solve() {
  vector< int > as(3);
  cin >> as;
  rwhole(sort, as);
  cout << as[0] + as[1] << endl;
}

signed main() {
  solve();
}
