stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))

using ll = long long;

using P = pair<int, int>;

using namespace std;



template<class T> void vin(vector<T>& v, int n) {

    v.resize(n);

    for (int i = 0; i < n; ++i) {

        cin >> v[i];

    }

}



int p[101010];

vector<int> g[101010];

bitset<101010> flag;



void dfs(int v, vector<int>& num, vector<int>& pos) {

    flag[v] = 1;

    pos.push_back(v+1);

    num.push_back(p[v]);

    for (int& u : g[v]) {

        if (flag[u]) continue;

        dfs(u, num, pos);

    }

}



int main() {

    int N, M;

    cin >> N >> M;

    rep(i, N) cin >> p[i];

    rep(i, M) {

        int x, y;

        cin >> x >> y;

        x--; y--;

        g[x].push_back(y);

        g[y].push_back(x);

    }

    int ans = 0;

    rep(i, N) {

        vector<int> n, p;

        if (!flag[i]) dfs(i, n, p);

        sort(n.begin(), n.end());

        sort(p.begin(), p.end());

        int idx1=0, idx2=0;

        while (true) {

            if (idx1 >= n.size() or idx2 >= p.size()) break;

            if (n[idx1] == p[idx2]) {

                idx1++; idx2++;

                ans++;

            }

            else if (n[idx1] > p[idx2]) {

                idx2++;

            }

            else if (n[idx1] < p[idx2]) {

                idx1++;

            }

        }

    }

    cout << ans << endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))

using ll = long long;

using P = pair<int, int>;

using namespace std;



template<class T> void vin(vector<T>& v, int n) {

    v.resize(n);

    for (int i = 0; i < n; ++i) {

        cin >> v[i];

    }

}



struct UnionFind {

    static const int MAX = 1000000;

    int par[MAX];

    void init(int N) {

        for (int i=0; i<N; ++i) {

            par[i] = -1;

        }

    }

    void init() {

        for (int i=0; i<MAX; ++i) {

            par[i] = -1;

        }

    }

    int find(int x) {

        if (par[x] < 0) return x;

        else return par[x] = find(par[x]);

    }

    bool unite(int x, int y) {

        x = find(x);

        y = find(y);

        if (x == y) return false;

        if (par[x] > par[y]) {

            int tmp = x;

            x = y;

            y = tmp;

        }

        par[x] += par[y];

        par[y] = x;

        return true;

    }

    bool same(int x, int y) {

        return find(x) == find(y);

    }

    // そのグループの木の大きさを返す

    int size(int x) {

        return -par[find(x)];

    }

};



int main() {

    int N, M;

    cin >> N >> M;

    vector<int> a(N);

    rep(i, N) {

        cin >> a[i]; a[i]--;

    }

    vector<int> pos(N);

    rep(i, N) pos[a[i]] = i;

    UnionFind uf;

    uf.init(N);

    rep(i, M) {

        int x, y; cin >> x >> y;

        x--; y--;

        uf.unite(x, y);

    }

    int ans = 0;

    rep(i, N) {

        if (uf.same(i, pos[i])) ans++;

    }

    cout << ans << endl;

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



const int mxn = 1e5+2;

vector<int> a1;

vector<int> a2;

vector<int> p(mxn);

bool vis[100001];

vector<int> g[100001];

vector<int> st(mxn);

int cnt = 0;

void dfs(int x){

	vis[x] = 1;

	if(st[x]>0) return;

	st[x] = cnt;

	for(auto to : g[x]){

		if(vis[to]) continue;

		dfs(to);

	}

}



int main(){

	int n,m;

	cin>>n>>m;

	for(int i = 1; i <= n; i++) cin>>p[i];

	for(int i = 0; i < m; i++){

		int a,b;

		cin>>a>>b;

		g[a].push_back(b);

		g[b].push_back(a);

	}



	for(int i = 1; i < n; i++){

		if(!vis[i]){

			cnt++;

			dfs(i);

		}

	}

    int res = 0;

	for(int i = 1; i <= n; i++){

		if(st[i]==st[p[i]]) res++;

	}

	 cout<<res;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;



const int mxn = 1e5+1;

vector<int> st(1e5+1);



int fs(int x){

	if(x == st[x]) return x;

	return st[x] = fs(st[x]);

}



void us(int x,int y){

	int px = fs(x);

	int py = fs(y);

	if(px!=py) st[py] = px;

}



int main(){

	int n,m;

	cin>>n>>m;



	vector<int> p(n+1);



	for(int i = 1; i <= n; i++) cin>>p[i];

	for(int i = 1; i <= n; i++) st[i] = i;



	for(int i = 0; i < m; i++){

		int x,y;

		cin>>x>>y;

		us(x,y);

	}

	int res = 0;

	for(int i = 1; i <= n; i++) if(fs(p[i]) == fs(i)) res++;

	cout<<res;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <algorithm>

#include <string>

#include <cmath>

#include <vector>

#include <queue>

#include <stack>

#include <utility>

#include <map>

using namespace std;



using ll = long long;



constexpr int limit = 1e5;

int n, m;



class V{

public:

  int tag;

  vector<V*> to;

};



int type[limit+1];

bool visited[limit+1];

int x[limit+1], y[limit+1];

V graph[limit+1];



void bfs(int, int);



int main(){

  cin >> n >> m;

  int p[n+1];

  for(int i=1; i<=n; i++) {

    cin >> p[i];

    graph[i].tag = i;

  }



  for(int i=1; i<=m; i++) {

    cin >> x[i] >> y[i];

    graph[x[i]].to.push_back(&graph[y[i]]);

    graph[y[i]].to.push_back(&graph[x[i]]);

  }



  int cnt=0;

  for(int i=1; i<=n; i++){

    if(!visited[i]){

      bfs(i, cnt);

      cnt++;

    }

  }



  int ans = n;

  for(int i=1; i<=n; i++){

    if(type[i] != type[p[i]]) ans--;

  }



  cout << ans << endl;



  return 0;

}



void bfs(int s, int t){

  type[s] = t;

  visited[s] = true;

  for(int i=0; i<graph[s].to.size(); i++){

    int nx = graph[s].to[i]->tag;

    if(!visited[nx]){

      bfs(nx, t);

    }

  }

}


// Optimized solution to the same problem (faster):
#include <iostream>

using namespace std;



/*

union_find class:

  int n;

  int *par, *rank;



  method: 

    union_find(int n);

    int find(int x);

    void unite(int x, int y);

    bool same(int x, int y);

    void print();

*/





class union_find{

public:

  int n;

  int *par, *rank;



  union_find(int n){

    this-> n = n;

    par = new int[n];

    rank = new int[n];

    init();

  }

  

  int find(int x){

    if(par[x] == x) return x;

    return par[x] = find(par[x]);

  }



  void unite(int x, int y){

    x = find(x);

    y = find(y);



    if(x == y) return;



    if(rank[x] < rank[y]){

      par[x] = y;

    }else{

      par[y] = x;

      if(rank[x] == rank[y]) rank[x]++;

    }

  }



  bool same(int x, int y){

    return find(x) == find(y);

  }



  int solve(){

    for(int i=0; i<n; i++){



    }

  }



  void print(){

    cout << "---------------" << endl;

    for(int i=0; i<n; i++){

      cout << "par " << i << " : " << par[i] << endl;

    }

    cout << "---------------" << endl;

  }



private:

  void init(){

    for(int i=0; i<n; i++){

      par[i] = i;

      rank[i] = 1;

    }

  }

};



int main(){

  int n, m; cin >> n >> m;

  union_find uf(n);

  int v[n];

  for(int i=0; i<n; i++) cin >> v[i];



  for(int i=0; i<m; i++){

    int x, y;

    cin >> x >> y;

    uf.unite(x-1, y-1);

  }  



  int cnt = 0;

  for(int i=0; i<n; i++){

    if(uf.same(i, v[i]-1)) cnt++;

  }



  cout << cnt << endl;

  /* uf.print(); */

  

  return 0; 

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>



#define INF 1e9

#define llINF 1e18

#define MOD 1e9+7

#define pb push_back

#define mp make_pair 

#define F first

#define Nodes 100100

#define S second

#define ll long long

using namespace std;

//union find{

#define MAX_N 100100

int par[MAX_N]; // 親

int Rank[MAX_N]; // 木の深さ



void init(int n){

  for(int i = 0; i < n; i++){

    par[i] = i;

    Rank[i] = 0;

  }

}

// 木の根を求める

int find(int x){

  if(par[x] == x)

    return x;

  else

    return par[x] = find(par[x]);

}

// xとyの属する集合を併合

void unite(int x, int y){

  x = find(x);

  y = find(y);

  if(x == y) return;

  

  // 木の高さが高い方を代表ノードにする

  if(Rank[x] < Rank[y]){

    par[x] = y;

  }else{

    par[y] = x;

    if(Rank[x] == Rank[y]) Rank[x]++;

  }

}

// xとyが同じ集合ならtrue

bool same(int x,int y){

  return find(x) == find(y);

}

int main(){

  int n,m;cin>>n>>m;

  vector<int>num(n);

  init(100010);

  for(int i=0;i<n;i++)cin>>num[i];

  for(int i=0;i<m;i++){

    int a,b;cin>>a>>b;

    unite(a,b);

  }

  ll ans=0;

  for(int i=0;i<n;i++){

    if(find(num[i])==find(i+1))ans++;

  }

  cout<<ans<<endl;

  return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define INF 1e9

#define llINF 1e18

#define MOD 1000000007

#define pb push_back

#define mp make_pair 

#define F first

#define S second

#define ll long long

#define vi vector<ll>

#define vvi vector<vi>

#define BITLE(n) (1LL<<((ll)n))

#define SHIFT_LEFT(n) (1LL<<((ll)n))

#define SUBS(s,f,t) ((s).substr((f),(t)-(f)))

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

using namespace std;

struct UF

{

  vector<int> par;

  vector<int> sz;

  vector<int> rank;

  UF(int n):par(n),sz(n),rank(n){

    for(int i = 0; i < n; i++){

      par[i] = i;sz[i] = 1;rank[i] = 0;

    }

  }

  int find(int x) {

    if (par[x] == x) return x;

    else return par[x] = find(par[x]);

  }

  void merge(int x, int y) {

    x = find(x); y = find(y);

    if (x == y) return;

    if(rank[x] < rank[y]){

      par[x] = y;

      sz[y] += sz[x];

    }

    else{

      par[y] = x;

      sz[x] += sz[y];

      if(rank[x] == rank[y])rank[x]++;

    }

  }

  bool same(int x, int y) { return find(x) == find(y); }

  int size(int n){return sz[find(n)];}

};



int main(){

  cin.tie(0);

  ios::sync_with_stdio(false);

  ll n,m;cin>>n>>m;

  UF uni(n);

  vi A(n);

  for(int i=0;i<n;i++){

    cin>>A[i];

    A[i]--;

  }

  for(int i=0;i<m;i++){

    ll a,b;cin>>a>>b;

    a--;b--;

    uni.merge(a,b);

  }

  ll ans = 0;

  for(int i=0;i<n;i++){

    ans += (uni.same(i,A[i]));

  }

  cout<<ans<<endl;

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define LL long long

#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define Re(a,b) memset(a,b,sizeof a)

#define F first

#define S second

#define MP make_pair

#define PB push_back

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

#define FOR(I, A, B) for (int I = (A); I <= (B); I++)

#define REP(I, N) for (int I = 0; I < (N); I++)

#define PII pair<int,int>

#define PLL pair<LL,LL>

#define SZ(a) ((int)a.size())

using namespace std;

const int N=1e5+10;

int p[N];

int find(int x){

	if(x==p[x])return p[x];

	return p[x]=find(p[x]);

}

int n,k;

int a[N];

vector<int>g[N];

set<int>s[N];

int main(){

	cin>>n>>k;

	FOR(i,1,n)cin>>a[i],p[i]=i;

	REP(i,k){

		int u,v;cin>>u>>v;u=find(u),v=find(v);

		p[u]=v;

	}

	int t=0;

	FOR(i,1,n){

		find(i);

		if(s[p[i]].find(i)!=s[p[i]].end()){

			t++;

		}

		else s[p[i]].insert(i);

		

		if(s[p[i]].find(a[i])!=s[p[i]].end()){

			t++;

		}

		else s[p[i]].insert(a[i]);

		

	}

	cout<<t<<endl;



}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define LL long long

#define Accel ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define Re(a,b) memset(a,b,sizeof a)

#define F first

#define S second

#define MP make_pair

#define PB push_back

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

#define FOR(I, A, B) for (int I = (A); I <= (B); I++)

#define REP(I, N) for (int I = 0; I < (N); I++)

#define PII pair<int,int>

#define PLL pair<LL,LL>

#define SZ(a) ((int)a.size())

using namespace std;

const int N=1e5+10;

int p[N];

int find(int x){

	if(x==p[x])return p[x];

	return p[x]=find(p[x]);

}

int n,k;

int a[N];

int main(){

	cin>>n>>k;

	FOR(i,1,n)cin>>a[i],p[i]=i;

	REP(i,k){

		int u,v;cin>>u>>v;u=find(u),v=find(v);

		p[u]=v;

	}

	int t=0;

	FOR(i,1,n){

		if(find(i)==find(a[i]))t++;

		

	}

	cout<<t<<endl;



}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> P;

typedef long long ll;

typedef long double ld;

const int inf=1e9+7;

const ll longinf=1LL<<60;

#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )

#define rep(i,n) REP(i,0,n)

#define F first

#define S second



const int mx=100010;

const ll mod=1e9+7;



vector<int> par(mx);



int find(int x){

  if(par[x]==x){ return x; } else { return par[x]=find(par[x]); }

}



void unite(int x, int y){

  x=find(x);

  y=find(y);

  par[y]=x;

}



int main(){

  int n,m,ans=0; 

  cin >> n >> m;

  vector<int> a(n);

  rep(i,n){ cin >> a[i]; a[i]--; }

  rep(i,n){ par[i] = i; }

  rep(i,m){ 

  	int x, y;

  	cin >> x >> y;

  	unite(x-1,y-1);

  }

  rep(i,n){if(find(i)==find(a[i])){ ans++; }}

  

  cout << ans << endl;

  return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> P;

typedef long long ll;

typedef long double ld;

const int inf=1e9+7;

const ll longinf=1LL<<60;

#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )

#define rep(i,n) REP(i,0,n)

#define F first

#define S second



const int mx=100010;

const ll mod=1e9+7;



struct UnionFind {  

  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int n) : par(n) { //最初は全てが根であるとして初期化

    for(int i=0; i<n; i++){ par[i]=i; }  

  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}

    if(par[x]==x){ return x; }

    return par[x] = root(par[x]);

  }

  void unite(int x, int y) { // xとyの木を併合

    int rx = root(x); //xの根をrx

    int ry = root(y); //yの根をry

    if(rx == ry){ return; }//xとyの根が同じ(=同じ木にある)時はそのまま

    par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける

  }



  bool check(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す

    return root(x) == root(y);

  }

};



int main() {

  int n,m; cin >> n >> m;

  UnionFind tree(n);

  vector<int> a(n);

  rep(i,n){ cin>>a[i]; a[i]--; }

  rep(i,m){

    int x,y; cin >> x >> y;

    tree.unite(x-1,y-1);

  }

  int ans=0;

  rep(i,n){ if(tree.check(i,a[i])){ ans++; } }

  

  cout << ans << endl;

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
# include <bits/stdc++.h>



# define pb push_back

# define fr first

# define sc second

# define mk make_pair



using namespace std;



const int inf = 1000000000 + 7;

const int N = 2e5 + 5;



typedef long long ll;



int n, mn1 = inf, mn2 = inf, mx1, mx2;

ll ans = 2e18;

pair <int, int> a[N];



int main()

{

    cin >> n;



    for(int i = 1; i <= n; i ++)

    {

        cin >> a[i].fr >> a[i].sc;

        if(a[i].fr > a[i].sc) swap(a[i].fr, a[i].sc);

        mn1 = min(a[i].fr, mn1);

        mn2 = min(a[i].sc, mn2);

        mx1 = max(a[i].fr, mx1);

        mx2 = max(a[i].sc, mx2);

    }



    ans = mx1 - mn1;

    ans *= (mx2 - mn2);



    sort(a + 1, a + n + 1);



    mx1 = mx2, mn2 = a[2].fr;



    ll l = 1e9, r = a[n].fr;

    for(int i = 1; i <= n; i ++)

    {

        l = min(l, a[i].sc * 1ll);

        r = max(r, a[i].sc * 1ll);

        ans = min(ans, (mx1 - mn1) * 1ll * (r - min(l, a[i + 1].fr * 1ll)));

    }

    cout << ans << endl;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

#include <vector>

#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)



inline int read()

{

	int data = 0, w = 1; char ch = getchar();

	while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();

	if (ch == '-') w = -1, ch = getchar();

	while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();

	return data * w;

}



const int maxn(2e5 + 10);

std::pair<int, int> t[maxn]; long long ans;

int n, lx[maxn], ly[maxn], rx[maxn], ry[maxn];



int main()

{

	scanf("%d", &n), ly[0] = ry[n + 1] = 1e9;

	for (int i = 1; i <= n; i++)

	{

	    scanf("%d%d", &t[i].first, &t[i].second);

	    if (t[i].first > t[i].second) std::swap(t[i].first, t[i].second);

	}

	std::sort(t + 1, t + n + 1);

	for (int i = 1; i <= n; i++) lx[i] = std::max(lx[i - 1], t[i].second);

	for (int i = 1; i <= n; i++) ly[i] = std::min(ly[i - 1], t[i].second);

	for (int i = n; i; i--) rx[i] = std::max(rx[i + 1], t[i].second);

	for (int i = n; i; i--) ry[i] = std::min(ry[i + 1], t[i].second);

	ans = 1ll * (t[n].first - t[1].first) * (lx[n] - ly[n]);

	for (int i = 1; i < n; i++)

	{

		int ax = std::max(t[n].first, lx[i]), ay = std::min(t[i + 1].first, ly[i]);

		int bx = std::max(rx[i + 1], t[i].first), by = std::min(ry[i + 1], t[1].first);

		ans = std::min(ans, 1ll * (ax - ay) * (bx - by));

	}

	printf("%lld\n", ans);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



#define pb push_back

#define mk make_pair

#define sc second

#define fr first



using namespace std;



typedef long long ll;



const long long N = (long long) 1e6 + 5;

const long long mod = (long long) 1e9 + 7;



pair<long long,long long> a[N];



long long n;

long long mn,mn1 = mod,mx,mx1,mnn,res,mxx;



int main () {

        ios_base::sync_with_stdio(false);



        cin >> n;



        for (long long i = 1; i <= n; i ++) {

                cin >> a[i].fr >> a[i].sc;

                if(a[i].fr > a[i].sc)

                        swap (a[i].fr,a[i].sc);

                mn1 = min (mn1,a[i].sc);

                mx1 = max (mx1,a[i].sc);

        }



        sort (a + 1,a + n + 1);



        mn = a[1].fr; mx = a[n].fr;



        res = (mx - mn) * 1ll * (mx1 - mn1);



        for (long long i = 1; i < n; i ++) {

                if(i > 1) mnn = min (mnn,a[i].sc);

                else mnn = a[i].sc;

                mxx = max (mxx,a[i].sc);



                res = min (res,(mx1 - mn) * 1ll * (max(mxx,a[n].fr) - min(a[i + 1].fr,mnn)));

        }

        cout << res << endl;

        return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

#include <vector>

#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)



inline int read()

{

	int data = 0, w = 1; char ch = getchar();

	while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();

	if (ch == '-') w = -1, ch = getchar();

	while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();

	return data * w;

}



const int maxn(2e5 + 10);

std::pair<int, int> t[maxn]; long long ans;

int n, lx[maxn], ly[maxn], rx[maxn], ry[maxn];



int main()

{

	scanf("%d", &n), ly[0] = ry[n + 1] = 1e9;

	for (int i = 1; i <= n; i++)

	{

	    scanf("%d%d", &t[i].first, &t[i].second);

	    if (t[i].first > t[i].second) std::swap(t[i].first, t[i].second);

	}

	std::sort(t + 1, t + n + 1);

	for (int i = 1; i <= n; i++) lx[i] = std::max(lx[i - 1], t[i].second);

	for (int i = 1; i <= n; i++) ly[i] = std::min(ly[i - 1], t[i].second);

	for (int i = n; i; i--) rx[i] = std::max(rx[i + 1], t[i].second);

	for (int i = n; i; i--) ry[i] = std::min(ry[i + 1], t[i].second);

	ans = 1ll * (t[n].first - t[1].first) * (lx[n] - ly[n]);

	for (int i = 1; i < n; i++)

	{

		int ax = std::max(t[n].first, lx[i]), ay = std::min(t[i + 1].first, ly[i]);

		int bx = std::max(rx[i + 1], t[i].first), by = std::min(ry[i + 1], t[1].first);

		ans = std::min(ans, 1ll * (ax - ay) * (bx - by));

	}

	printf("%lld\n", ans);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX = (1e5)+1;

 int n;

vector<pair<ll, ll> > a;

multiset<ll> r, b;

long long ans = LLONG_MAX;

int main() {	

ios::sync_with_stdio(false);

cin.tie(0);	

cin >> n;

a.resize(n);	

for(int i = 0; i < n; i++) {	

	cin >> a[i].first >> a[i].second;		if(a[i].first > a[i].second) 

swap(a[i].first, a[i].second);

	}	

sort(a.begin(), a.end());

	for(auto p: a) {	

	r.insert(p.first);	

	b.insert(p.second);	}	

ans = min((*r.rbegin() - *r.begin()) * (*b.rbegin() - *b.begin()), ans);	

for(auto p: a) {	

	r.erase(r.find(p.first));		b.erase(b.find(p.second));		r.insert(p.second);	

	b.insert(p.first);	

	ans = min((*r.rbegin() - *r.begin()) * (*b.rbegin() - *b.begin()), ans);	}

	cout << ans;}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

#include <vector>

#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)



inline int read()

{

	int data = 0, w = 1; char ch = getchar();

	while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();

	if (ch == '-') w = -1, ch = getchar();

	while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();

	return data * w;

}



const int maxn(2e5 + 10);

std::pair<int, int> t[maxn]; long long ans;

int n, lx[maxn], ly[maxn], rx[maxn], ry[maxn];



int main()

{

	scanf("%d", &n), ly[0] = ry[n + 1] = 1e9;

	for (int i = 1; i <= n; i++)

	{

	    scanf("%d%d", &t[i].first, &t[i].second);

	    if (t[i].first > t[i].second) std::swap(t[i].first, t[i].second);

	}

	std::sort(t + 1, t + n + 1);

	for (int i = 1; i <= n; i++) lx[i] = std::max(lx[i - 1], t[i].second);

	for (int i = 1; i <= n; i++) ly[i] = std::min(ly[i - 1], t[i].second);

	for (int i = n; i; i--) rx[i] = std::max(rx[i + 1], t[i].second);

	for (int i = n; i; i--) ry[i] = std::min(ry[i + 1], t[i].second);

	ans = 1ll * (t[n].first - t[1].first) * (lx[n] - ly[n]);

	for (int i = 1; i < n; i++)

	{

		int ax = std::max(t[n].first, lx[i]), ay = std::min(t[i + 1].first, ly[i]);

		int bx = std::max(rx[i + 1], t[i].first), by = std::min(ry[i + 1], t[1].first);

		ans = std::min(ans, 1ll * (ax - ay) * (bx - by));

	}

	printf("%lld\n", ans);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
# include <bits/stdc++.h>

# define f first

# define s second



using namespace std;



const int N = 1e6;



long long n, x, y, ans;

pair <long long, long long> a[N], b[N];



int main(){

      scanf("%lld", &n);

      for(int i = 1;i <= n;i ++){

             scanf("%lld %lld", &a[i].f, &a[i].s);

             if(a[i].f > a[i].s)

                 swap(a[i].s, a[i].f);

             b[i].f = a[i].s, b[i].s = a[i].f;

      }



      sort(b + 1, b + n + 1);

      sort(a + 1, a + n + 1);



      ans = (a[n].f - a[1].f) * (b[n].f - b[1].f);

      long long r = a[n].f, l = 1e9;

      for(int i = 1;i < n;i ++){

             l = min(a[i].s, l);

             r = max(r, a[i].s);

             ans = min(ans, (b[n].f - a[1].f) * (r - min(l, a[i + 1].f)));

      }

      cout << ans;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

#include <vector>

#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)



inline int read()

{

	int data = 0, w = 1; char ch = getchar();

	while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();

	if (ch == '-') w = -1, ch = getchar();

	while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();

	return data * w;

}



const int maxn(2e5 + 10);

std::pair<int, int> t[maxn]; long long ans;

int n, lx[maxn], ly[maxn], rx[maxn], ry[maxn];



int main()

{

	scanf("%d", &n), ly[0] = ry[n + 1] = 1e9;

	for (int i = 1; i <= n; i++)

	{

	    scanf("%d%d", &t[i].first, &t[i].second);

	    if (t[i].first > t[i].second) std::swap(t[i].first, t[i].second);

	}

	std::sort(t + 1, t + n + 1);

	for (int i = 1; i <= n; i++) lx[i] = std::max(lx[i - 1], t[i].second);

	for (int i = 1; i <= n; i++) ly[i] = std::min(ly[i - 1], t[i].second);

	for (int i = n; i; i--) rx[i] = std::max(rx[i + 1], t[i].second);

	for (int i = n; i; i--) ry[i] = std::min(ry[i + 1], t[i].second);

	ans = 1ll * (t[n].first - t[1].first) * (lx[n] - ly[n]);

	for (int i = 1; i < n; i++)

	{

		int ax = std::max(t[n].first, lx[i]), ay = std::min(t[i + 1].first, ly[i]);

		int bx = std::max(rx[i + 1], t[i].first), by = std::min(ry[i + 1], t[1].first);

		ans = std::min(ans, 1ll * (ax - ay) * (bx - by));

	}

	printf("%lld\n", ans);

	return 0;

}


// Fastest known solution for this problem: