#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define REP(i,a,b) for(int i=int(a);i<(int)b;++i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vp vector<pair<ll,ll>>
#define pb push_back
#define all(v) v.begin(),v.end()


using namespace std;
using ll = long long;
const ll INF = 1e18;
const double PI = acos(-1);

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int main(void){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vi p(n),dif(n);
    ll num = 0;
    vi ans(n),ta(200000);
    set<int> se;
    
    rep(i,n){
        cin >> p[i];
        se.insert(p[i]);
        ta[p[i]]++;

        

    }
    int i = 0;
    while(true){
        if(se.count(i) == 0){
            ans[n-1] = i; 
            break;
        }
        ++i;
    }

    for(int j = n-2;j >= 0; --j){
        ta[p[j+1]]--;
        if(p[j+1] < ans[j+1] && !ta[p[j+1]]){
            ans[j] = p[j+1];
        }else{
            ans[j] = ans[j+1];
        }
    }
    rep(i,n){
        cout << ans[i] << endl;
    }

    return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll> pql;
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, k, n) for(ll i = ll(k); i <= ll(n); i++)
#define rep3(i, n, k) for(ll i = ll(n); i >= ll(k); i--)
#define all(v) (v).begin(), (v).end()
ll mod(ll a, ll b) {return (a % b + b) % b;}
ll quo(ll a, ll b) {return (a - mod(a, b)) / b;}
template <typename T, typename U> bool chmin(T &a, const U b) {if(a > b) {a = b; return 1;} return 0;}
template <typename T, typename U> bool chmax(T &a, const U b) {if(a < b) {a = b; return 1;} return 0;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll MAX = 2e5;
const ld eps = 1e-9;
const char newl = '\n';

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

  ll n, x, c;
  cin >> n;
  vl l(n, INF), r(n, -INF);
  rep(_, n) {
    cin >> x >> c;
    c--;
    chmin(l[c], x);
    chmax(r[c], x);
  }
  pl tmpl = pl(0, 0), tmpr = pl(0, 0);
  rep(i, n) {
    if(l[i] > r[i]) continue;
    pl L = pl(l[i], 0), R = pl(r[i], 0);
    L.second = min(tmpl.second + abs(tmpl.first - r[i]), tmpr.second + abs(tmpr.first - r[i])) + abs(r[i] - l[i]);
    R.second = min(tmpl.second + abs(tmpl.first - l[i]), tmpr.second + abs(tmpr.first - l[i])) + abs(l[i] - r[i]);
    tmpl = L;
    tmpr = R;
  }
  cout << min(abs(tmpl.first) + tmpl.second, abs(tmpr.first) + tmpr.second) << newl;
  return 0; 
}