#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;

void bmain()
{
    int n; cin >> n;
    long c[n][n];
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++){
            cin >> c[i][j];
        }
    }
    long a0[n];
    for (int i=0;i<n;i++) a0[i]=c[0][i];
    long b0[n];
    b0[0]=0;
    for (int i=1;i<n;i++){
        b0[i]=c[i][0]-c[0][0];
        for (int j=1;j<n;j++){
            if (c[i][j]-a0[j]!=b0[i]){
                cout << "No" << endl;
                return;
            }
        }
    }
    long minb=b0[0];
    for (int i=1;i<n;i++) minb=min(b0[i],minb);
    if (minb<0){
        for (int i=0;i<n;i++){
            b0[i]-=minb;
            a0[i]+=minb;
            if (a0[i]<0){
                cout << "No" << endl;
                return;
            }
        }
    }
    cout << "Yes" << endl;
    for (int i=0;i<n;i++) cout << b0[i] << " ";
    cout << endl;
    for (int j=0;j<n;j++) cout << a0[j] << " ";
    cout << endl;
    return;
}

void amain()
{
    int n,m; cin >> n >> m;
    string s;
    long x[2];
    x[0]=0;
    x[1]=0;
    for (int i=0;i<n;i++) {
        cin>>s;
        int cpt=0;
        for (int j=0;j<m;j++){
            if (s[j]=='1') cpt++;
        }
        x[cpt%2]++;
    }
    cout << x[0]*x[1] << endl;
    return;
}

int main()
{
    ios_base::sync_with_stdio(0);
    bmain();
    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = 1; i <= (int)(n); i++)
#define drep(i, n) for(int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define dup(x,y) (((x)+(y)-1)/(y))
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vs = vector<string>;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const int mod = 1000000007;
// const int mod = 998244353;



int main() {
  int n;
  cin >> n;
  vi l(n, INF), r(n, -INF);
  rep(i, n) {
    int x, c;
    cin >> x >> c;
    --c;
    chmin(l[c], x);
    chmax(r[c], x);
  }
  vector<P> d;
  d.emplace_back(0,0);
  rep(i, n) if (l[i] != INF) d.emplace_back(l[i], r[i]);
  d.emplace_back(0,0);
  vl dp(2);
  for (int i = 1; i < d.size(); ++i) {
    vl p(2, LINF);
    swap(p, dp);
    int l = d[i].first, r = d[i].second;
    rep(j, 2) {
      int x = j ? d[i-1].second : d[i-1].first;
      chmin(dp[0], p[j] + abs(x-r) + r-l);
      chmin(dp[1], p[j] + abs(x-l) + r-l);
    }
  }
  cout << dp[0] << endl;
}