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

// Type alias
using ll = long long;
using ld = double;

using pi = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
using ti3 = tuple<int, int, int>;

using vi = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpi = vector<pi>;
using vpll = vector<ll>;
using vpld = vector<ld>;
using vti3 = vector<ti3>;

// Constants
const double EPS = 1e-9;
const int inf = numeric_limits<int>::max() / 2;
const ll llinf = numeric_limits<ll>::max() / 2;
const ll mod = 1e9 + 7;

// Macro
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rrep(i, n) for (int i = int(n - 1); i >= 0; --i)
#define REP(i, a, b) for (int i = int(a); i < int(b); ++i)
#define RREP(i, a, b) for (int i = int(b - 1); i >= int(a); --i)
#define SHOW(a) cout << #a << " = " << a << endl
#define ARR(a, n)                                                              \
  for (int i = 0; i < int(n); ++i)                                             \
  cout << #a << "[" << i << "]"                                                \
       << " = " << a[i] << endl

#define ALL(a) a.begin(), a.end()

// Funtcions
ll pow(ll base, ll i, ll mod) {
  ll a = 1;
  while (i) {
    if (i & 1) {
      a *= base;
      a %= mod;
    }
    base *= base;
    base %= mod;
    i >>= 1;
  }
  return a;
}
ll gcd(ll a, ll b) {
  while (b) {
    ll c = a % b;
    a = b;
    b = c;
  }
  return a;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b)
    return a = b, true;
  else
    return false;
}

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b)
    return a = b, true;
  else
    return false;
}

struct hoge {
  ll color, left, right;
};

void solve() {
  ll n;
  cin >> n;
  pll xc[200000 + 5];
  rep(i, n) {
    ll t, u;
    cin >> t >> u;
    xc[i] = make_pair(u, t);
  }

  sort(begin(xc), xc + n);

  vector<hoge> a;
  hoge b;
  b.color = xc[0].first;
  b.left = xc[0].second;
  a.push_back(b);
  for (ll i = 1; i < n; i++) {
    if (xc[i - 1].first != xc[i].first) {
      a[a.size() - 1].right = xc[i - 1].second;
      hoge b;
      b.color = xc[i].first;
      b.left = xc[i].second;
      a.push_back(b);
    }
  }
  a[a.size() - 1].right = xc[n - 1].second;

  vector<vector<pll>> d(a.size(), vector<pll>(2, make_pair(llinf, 0)));
  d[0][0] = make_pair(abs(a[0].left) + abs(a[0].right - a[0].left), a[0].right);
  d[0][1] = make_pair(abs(a[0].right) + abs(a[0].left - a[0].right), a[0].left);

  for (ll i = 1; i < a.size(); i++) {
    d[i][0] =
        make_pair(min(d[i - 1][0].first + abs(d[i - 1][0].second - a[i].left) +
                          abs(a[i].left - a[i].right),
                      d[i - 1][1].first + abs(d[i - 1][1].second - a[i].left) +
                          abs(a[i].left - a[i].right)),
                  a[i].right);
    d[i][1] =
        make_pair(min(d[i - 1][0].first + abs(d[i - 1][0].second - a[i].right) +
                          abs(a[i].left - a[i].right),
                      d[i - 1][1].first + abs(d[i - 1][1].second - a[i].right) +
                          abs(a[i].left - a[i].right)),
                  a[i].left);
  }

  cout << min(d[a.size() - 1][0].first + abs(d[a.size() - 1][0].second),
              d[a.size() - 1][1].first + abs(d[a.size() - 1][1].second))
       << endl;
}

int main() {
  // FastIO
  // ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

#define int ll
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;

template<typename T, typename U>
ostream& operator<<(ostream& o, const pair<T,U>& p) {
  return o << "(" << p.first << "," << p.second << ")";
}

template<typename T, typename U>
ostream& operator<<(ostream& o, const map<T,U>& m) {
  o << "{";
  for (const pair<T,U>& p : m)
    o << p.first << ": " << p.second << ",";
  return o << "}";
}

template<typename T>
ostream& operator<<(ostream& o, const set<T>& s) {
  o << "{";
  for (const T& e : s)
    o << s << ",";
  return o << "}";
}

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

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

const int INF = 1e18;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int a, b;
  cin >> a >> b;
  if (a == b) {
    for (int i=1; i<=a; ++i)
      cout << i << " -" << i << " ";
    cout << "\n";
    return 0;
  }
  string bsign = "-";
  string asign = "";
  if (a > b) {
    swap(a, b);
    swap(asign, bsign);
  }
  assert(a < b);
  for (int i=1; i<=b; ++i)
    cout << bsign << i << " ";
  for (int i=1; i<a; ++i)
    cout << asign << i << " ";
  int sum = 0;
  for (int i=a; i<=b; ++i)
    sum += i;
  cout << asign << sum << "\n";
}
