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

int maxglobal = -1;

struct Vruh{
    int segch = -1;
    bool flag = false;
    vector<pair<int, int>> sasedi;
};

const int maxn = 2002;
int n;
pair<int, pair<int, int>> a[maxn];
Vruh v[maxn];
vector<int> nachalaZaBfs;

void bfs1(){
    //                 segch ind
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > pq;

    for(int i=0;i<n;i++){
        if(v[i].flag == false){
            vector<int> mins;
            int segmin = 4;

            v[i].segch = 0;
            v[i].flag = true;
            pq.push(make_pair(v[i].segch, i));

            while(pq.empty() == false){
                pair<int, int> s = pq.top();
                pq.pop();

                if(v[s.second].sasedi.size() < segmin){
                    mins.clear();
                    segmin = v[s.second].sasedi.size();
                }
                if(v[s.second].sasedi.size() == segmin){
                    mins.push_back(s.second);
                }

                for(auto j = v[s.second].sasedi.begin(); j < v[s.second].sasedi.end(); j++){
                    if(v[(*j).first].flag == false){
                        pq.push(make_pair(v[s.second].segch + (*j).second, (*j).first));
                        v[(*j).first].flag = true;
                    }
                }
            }

            for(int f=0;f<mins.size();f++){
                nachalaZaBfs.push_back(mins[f]);
            }

        }

    }
}

void bfs2(){

    //                 segch ind
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > pq;
    for(auto i = nachalaZaBfs.begin(); i < nachalaZaBfs.end(); i++){
        if(v[*i].flag == true){
            for(int f=0;f<n;f++){
                v[f].flag = false;
                v[f].segch = -1;
            }
        }

        if(v[*i].flag == false) {
            v[*i].segch = 0;
            v[*i].flag = true;
            pq.push(make_pair(v[*i].segch, *i));

            while(pq.empty() == false){
                pair<int, int> s = pq.top();
                pq.pop();
                if(s.first > maxglobal) maxglobal = s.first;
                //cout << s.first << " " << s.second + 1 << endl;

                for(auto j = v[s.second].sasedi.begin(); j < v[s.second].sasedi.end(); j++){
                    if(v[(*j).first].flag == false){
                        pq.push(make_pair(v[s.second].segch + (*j).second, (*j).first));
                        v[(*j).first].segch = v[s.second].segch + (*j).second;
                        v[(*j).first].flag = true;
                    }
                }
            }
        }

    }
}


int main() {
    //cout << "--- zad3 ---" << endl;
    cin.tie(0);
    cout.tie(0);

    cin >> n;
    for(int i=0;i<n;i++){
        cin >> a[i].first >> a[i].second.first;
        a[i].second.second = i;
    }

    struct {
        bool operator()(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) const
        {
            if(a.first == b.first) return a.second.first < b.second.first;
            return a.first < b.first;
        }
    } customLess;
    sort(a, a + n, customLess);
    for(int i=1;i<n;i++){
        if(a[i - 1].first == a[i].first){
            if(a[i - 1].second.first != a[i].second.first){
                v[a[i - 1].second.second].sasedi.push_back(make_pair(a[i].second.second, abs(a[i - 1].second.first - a[i].second.first)));
                v[a[i].second.second].sasedi.push_back(make_pair(a[i - 1].second.second, abs(a[i - 1].second.first - a[i].second.first)));
            }
        }
    }


    struct {
    bool operator()(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) const
    {
        if(a.second.first == b.second.first) return a.first < b.first;
        return a.second.first < b.second.first;
    }
    } customLess2;
    sort(a, a + n, customLess2);
    for(int i=1;i<n;i++){
        if(a[i - 1].second.first == a[i].second.first){
            if(a[i - 1].first != a[i].first){
                v[a[i - 1].second.second].sasedi.push_back(make_pair(a[i].second.second, abs(a[i - 1].first - a[i].first)));
                v[a[i].second.second].sasedi.push_back(make_pair(a[i - 1].second.second, abs(a[i - 1].first - a[i].first)));
            }
        }
    }



    //bfs1();
    //for(int i=0;i<n;i++){
        //v[i].flag = false;
        //v[i].segch = -1;
    //}
    for(int i=0;i<n;i++){
        nachalaZaBfs.push_back(i);
    }

    bfs2();

    cout << maxglobal;

    return 0;
}
