#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define V vector
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define sz(x) int(x.size())
#define pcnt __builtin_popcountll
template <typename T> bool chmin(T &a, const T &b) {if(a > b){a = b; return true;} return false;}
template <typename T> bool chmax(T &a, const T &b) {if(a < b){a = b; return true;} return false;}
template <typename T> void print(T a) {cout << a << ' ';}
template <typename T> void printe(T a) {cout << a << endl;}
template <typename T> void printv(T a) {rep(i, sz(a))print(a[i]); cout<<endl;}
template <typename T> void printp(pair<T, T> a) {print(a.first); cout<<a.second<<endl;}
template <typename A, size_t N, typename T> void Fill (A (&array)[N], const T & val) {fill ((T*)array, (T*)(array+N), val);}
template <typename T> using vc = vector<T>;
template <typename T> using vv = vc<vc<T>>;
template <typename T> using vvv = vc<vv<T>>;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vc<int>;
using vvi = vv<int>;
using vl = vc<ll>;
using vvl = vv<ll>;
vi di = {-1, 1,  0, 0, -1, -1,  1, 1};
vi dj = { 0, 0, -1, 1, -1,  1, -1, 1};

int main () {
    int n; cin >> n;
    vvl a(n, vl(5)); rep(i, n) rep(j, 5) cin >> a[i][j];
    ll ok = 0, ng = 1e10;
    while (abs(ok - ng) > 1) {
        ll mid = (ok + ng) / 2;
        vv<bool> flag(n, vc<bool>(5));
        rep(i, n) rep(j, 5) flag[i][j] = (a[i][j] >= mid);
        vi dp(1<<5, 10000);
        dp[0] = 0;
        rep(i, n) {
            rep(s, 1 << 5) {
                int t = s;
                rep(j, 5) if (flag[i][j]) t |= (1 << j);
                chmin(dp[t], dp[s] + 1);
            }
        }
        if (dp[(1<<5) - 1] <= 3) ok = mid;
        else ng = mid;
    }
    cout << ok << endl;
    return 0;
}
#include <algorithm>
#include <iostream>
#include <deque>
#include <climits>
#include <cmath>
#include <map>

using namespace std;

struct S {
    int left;
    int right;
};


int main() {
    int N;
    std::cin >> N;

    std::map<int, S> colors;
    for (int i = 0; i < N; i++) {
        int x, c;
        std::cin >> x >> c;
        if (colors.find(c) == colors.end()) {
            S s;
            s.left = x;
            s.right = x;
            colors[c] = s;
            continue;
        }
        if (x < colors[c].left) {
            colors[c].left = x;
        }
        if (x > colors[c].right) {
            colors[c].right = x;
        }
    }

    int left = 0;
    int64_t cleft = 0;
    int right = 0;
    int64_t cright = 0;

    for (auto pair : colors) {
        auto it = pair.second;
        // cout << pair.first << ' ' << it.left << ' ' << it.right << endl;
        int64_t cleft1 = cleft + abs(left - it.right) + abs(it.right - it.left);
        int64_t cleft2 = cright + abs(right - it.right) + abs(it.right - it.left);
        int64_t cright1 = cleft + abs(left - it.left) + abs(it.right - it.left);
        int64_t cright2 = cright + abs(right - it.left) + abs(it.right - it.left);
        cleft = cleft1 < cleft2 ? cleft1 : cleft2;
        cright = cright1 < cright2 ? cright1 : cright2;
        left = it.left;
        right = it.right;
    }

    cleft += abs(left);
    cright += abs(right);
    int64_t result = cleft < cright ? cleft : cright;



    std::cout << result << endl;

    return 0;
}
