// Author : Sarthak Kapoor
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define repba(i,b,a) for(int i=b;i>=a;--i)
#define repab(i,a,b) for(int i=a;i<b;++i)
#define ll long long
#define pi pair<int,int>
#define pii pair<pi,int>
#define ull unsigned long long
#define vi vector<int>
#define viip vector<pair<int,pair<int,int> > >
#define vip vector<pair<int,int> >
#define pb push_back
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}
ll fastexp(ll x,ll a){
    ll res = 1;
    while(a>0){
        if(a&1){
            res = (res*x)%mod;
        }
        a=a>>1;
        x=(x*x)%mod;
    }
    return res;
}
ll inverse(ll n){
    return fastexp(n,mod-2);
}
template <typename T>
void add(T &a, T b){
	a += b;
	if(a >= mod)a -= mod;
}
template <typename T>
void sub(T &a, T b){
	a -= b;
	if(a < 0)a += mod;
}
template <typename T>
void mul(T &a, T b){
	a *= b;
	if(a >= mod)a %= mod;
}
const int maxn = 2e5 + 2;
vi v[maxn];
int d[maxn][3];
map<int,int> mp;
int in[maxn];
bool vis[maxn];
vip res;
vi path;
ll ans = 0;
void dfs(int u,int p,int id){
	for(auto x:v[u]){
		if(x != p){
			d[x][id] = d[u][id]+1;
			dfs(x,u,id);
		}
	}
}
void find_path(int start,int end){
	int ver = start;
	while(ver != end){
		mp[ver] = 1;
		path.pb(ver);
		for(auto x:v[ver]){
			if(d[x][2]+1 == d[ver][2]){
				ver = x;
				break;
			}
		}
	}
	mp[ver] = 1;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	rep(i,n-1){
		int a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
		in[a]++;
		in[b]++;
	}
	dfs(1,1,0);
	int dis = -1;
	int pos;
	repa(i,1,n+1){
		if(dis < d[i][0]){
			dis = d[i][0];
			pos = i;
		}
	}
	// cout<<pos<<endl;
	dfs(pos,pos,1);
	int end1 = pos;
	int end2;
	dis = -1;
	repa(i,1,n+1){
		if(dis < d[i][1]){
			end2 = i;
			dis = d[i][1];
		}
	}
	dfs(end2,end2,2);
	find_path(end1,end2);
	set<pi > m;
	// cout<<end1<<" "<<end2<<endl;
	repa(i,1,n+1){
		if(!mp[i]){
			m.insert({in[i],i});
		}
	}
	while(!m.empty()){
		pi x = *m.begin();
		int u = x.sec;
		// cout<<u<<endl;
		assert(x.fi == 1);
		m.erase(x);
		vis[u] = 1;
		for(auto y:v[u]){
			// vis[y] = 1;
			if(!vis[y]){
				if(!mp[y])m.erase({in[y],y});
				in[y]--;
				if(!mp[y])m.insert({in[y],y});
			}
		}
		int max1 = max(d[u][1],d[u][2]);
		ans += max1;
		if(max1 == d[u][1]){
			res.pb({u,end1});
		}
		else{
			res.pb({u,end2});
		}
	}
	// repa(i,1,n+1){
	// 	if(mp[i]){
	// 		m.insert({in[i],i});
	// 	}
	// }
	// while(!m.empty()){
	// 	int x = *m.begin();
	// 	int u = x.sec;
	// 	vis[u] = 1;
	// 	assert(x.fi == 1);
	// 	for(auto y:v[u]){
	// 		if(!vis[y]){
	// 			m.erase({in[y],y});
	// 			in[y]--;
	// 			m.insert({in[y],y});
	// 		}
	// 	}
	// }
	// do something else
	int sz = s(path);
	rep(i,sz){
		ans += d[path[i]][2];
		res.pb({path[i],end2});
	}
	cout<<ans<<"\n";
	for(auto x:res){
		cout<<x.fi<<" "<<x.sec<<" "<<x.fi<<"\n";
	}
	return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

int n;
basic_string<int> e[200005];
int d[200005], p[200005];

void bfs(int x) {
	queue<int> q;
	q.push(x);
	fill(d+1, d+n+1, -1);
	d[x] = 0;
	p[x] = -1;
	while (q.size()) {
		int x = q.front(); q.pop();
		for (int y : e[x]) {
			if (d[y] == -1) {
				d[y] = d[x] + 1;
				p[y] = x;
				q.push(y);
			}
		}
	}
}

void dfs(int x, int p, int dub, map<int, basic_string<int>>& mp) {
	mp[dub] += x;
	for (int y : e[x]) {
		if (y != p) {
			dfs(y, x, dub+1, mp);
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.tie(nullptr);

	cin >> n;
	for (int i=1; i<n; i++) {
		int u, v;
		cin >> u >> v;
		e[u] += v;
		e[v] += u;
	}

	bfs(1);
	int f = max_element(d+1, d+n+1) - d;
	bfs(f);
	int g = max_element(d+1, d+n+1) - d;
	int diam = d[g];
	for (int i=0; i<diam/2; i++) {
		g = p[g];
	}

	// cerr << "root: " << g << '\n';

	vector<map<int, basic_string<int>>> v;
	for (int y : e[g]) {
		map<int, basic_string<int>> mp;
		dfs(y, g, 1, mp);
		v.emplace_back(move(mp));
	}

	{
		map<int, basic_string<int>> xd;
		xd[0] = {g};
		v.emplace_back(move(xd));
	}

	set<pair<int, int>> st;
	int k = v.size();
	for (int i=0; i<k; i++) {
		st.insert({v[i].rbegin()->first, i});
	}

	basic_string<pair<int, int>> sol;

	auto izvadi_cvor = [&](int x, bool brisi) {
		if (brisi) {
			auto it = prev(v[x].end());
			if (it->second.size() == 1) {
				st.erase({it->first, x});
				if (it->first > 1)
					st.insert({it->first-1, x});
				int r = it->second[0];
				v[x].erase(it);
				return r;
			} else {
				int r = it->second.back();
				it->second.pop_back();
				return r;
			}
		} else {
			auto it = prev(v[x].end());
			int r = it->second.back();
			return r;
		}
	};

	ll z = 0;

	while (st.size() >= 2) {

		// stapmaj stanje mape
		// cerr << "vector size: " << v.size() << '\n';
		// cerr << "vector elements:\n";
		// for (int i=0; i<(int)v.size(); i++) {
		// 	cerr << i << ":\n";
		// 	for (auto [x, w] : v[i]) {
		// 		cerr << "  key = " << x << ", value =";
		// 		for (int y : w)
		// 			cerr << ' ' << y;
		// 		cerr << '\n';
		// 	}
		// }

		auto it1 = prev(st.end());
		auto it2 = prev(it1);

		z += it1->first + it2->first;

		int zadnji = it1->second;
		int predzadnji = it2->second;

		int x = izvadi_cvor(zadnji, true);
		int y = izvadi_cvor(predzadnji, false);

		sol += {x, y};
	}

	cout << z << '\n';
	for (auto [x, y] : sol)
		cout << x << ' ' << y << ' ' << x << '\n';
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

int n;
basic_string<int> e[200005];
int d1[200005], p[200005], d2[200005], f, g, h;

void bfs(int x, int* d) {
	queue<int> q;
	q.push(x);
	fill(d+1, d+n+1, -1);
	d[x] = 0;
	p[x] = -1;
	while (q.size()) {
		int x = q.front(); q.pop();
		for (int y : e[x]) {
			if (d[y] == -1) {
				d[y] = d[x] + 1;
				p[y] = x;
				q.push(y);
			}
		}
	}
}

bool on_diam[200005];

ll z;
basic_string<pair<int, int>> sol;

void dfs_brate(int x, int p) {
	for (int y : e[x]) {
		if (y != p) {
			dfs_brate(y, x);
		}
	}
	if (!on_diam[x]) {
		if (d1[x] > d2[x]) {
			z += d1[x];
			sol += {x, f};
		} else {
			z += d2[x];
			sol += {x, g};
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.tie(nullptr);

	cin >> n;
	for (int i=1; i<n; i++) {
		int u, v;
		cin >> u >> v;
		e[u] += v;
		e[v] += u;
	}

	bfs(1, d1);
	f = max_element(d1+1, d1+n+1) - d1;
	bfs(f, d1);
	g = max_element(d1+1, d1+n+1) - d1;
	int diam = d1[g];
	h = g;
	basic_string<int> dnodes;
	for (int i=0; i<diam; i++) {
		on_diam[h] = 1;
		dnodes += h;
		h = p[h];
	}
	on_diam[f] = 1;
	bfs(g, d2);

	dfs_brate(f, f);

	for (int i=0; i<diam; i++) {
		z += diam - i;
		sol += {dnodes[i], f};
	}

	cout << z << '\n';
	for (auto [x, y] : sol)
		cout << x << ' ' << y << ' ' << x << '\n';
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

int n;
basic_string<int> e[200005];
int d[200005], p[200005];

void bfs(int x) {
	queue<int> q;
	q.push(x);
	fill(d+1, d+n+1, -1);
	d[x] = 0;
	p[x] = -1;
	while (q.size()) {
		int x = q.front(); q.pop();
		for (int y : e[x]) {
			if (d[y] == -1) {
				d[y] = d[x] + 1;
				p[y] = x;
				q.push(y);
			}
		}
	}
}

void dfs(int x, int p, int dub, map<int, basic_string<int>>& mp) {
	mp[dub] += x;
	for (int y : e[x]) {
		if (y != p) {
			dfs(y, x, dub+1, mp);
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.tie(nullptr);

	cin >> n;
	for (int i=1; i<n; i++) {
		int u, v;
		cin >> u >> v;
		e[u] += v;
		e[v] += u;
	}

	bfs(1);
	int f = max_element(d+1, d+n+1) - d;
	bfs(f);
	int g = max_element(d+1, d+n+1) - d;
	int diam = d[g];
	for (int i=0; i<diam/2; i++) {
		g = p[g];
	}

	vector<map<int, basic_string<int>>> v;
	for (int y : e[g]) {
		map<int, basic_string<int>> mp;
		dfs(y, g, 1, mp);
		v.emplace_back(move(mp));
	}

	{
		map<int, basic_string<int>> xd;
		xd[0] = {1};
		v.emplace_back(move(xd));
	}

	set<pair<int, int>> st;
	int k = v.size();
	for (int i=0; i<k; i++) {
		st.insert({v[i].rbegin()->first, i});
	}

	basic_string<pair<int, int>> sol;

	auto izvadi_cvor = [&](int x, bool brisi) {
		if (brisi) {
			auto it = prev(v[x].end());
			if (it->second.size() == 1) {
				st.erase({it->first, x});
				if (it->first > 1)
					st.insert({it->first-1, x});
				int r = it->second[0];
				v[x].erase(it);
				return r;
			} else {
				int r = it->second.back();
				it->second.pop_back();
				return r;
			}
		} else {
			auto it = prev(v[x].end());
			int r = it->second.back();
			return r;
		}
	};

	ll z = 0;

	while (st.size() >= 2) {
		auto it1 = prev(st.end());
		auto it2 = prev(it1);

		z += it1->first + it2->first;

		int zadnji = it1->second;
		int predzadnji = it2->second;

		int x = izvadi_cvor(zadnji, true);
		int y = izvadi_cvor(predzadnji, false);

		sol += {x, y};
	}

	cout << z << '\n';
	for (auto [x, y] : sol)
		cout << x << ' ' << y << ' ' << x << '\n';
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

int n;
basic_string<int> e[200005];
int d1[200005], p[200005], d2[200005], f, g, h;

void bfs(int x, int* d) {
	queue<int> q;
	q.push(x);
	fill(d+1, d+n+1, -1);
	d[x] = 0;
	p[x] = -1;
	while (q.size()) {
		int x = q.front(); q.pop();
		for (int y : e[x]) {
			if (d[y] == -1) {
				d[y] = d[x] + 1;
				p[y] = x;
				q.push(y);
			}
		}
	}
}

bool on_diam[200005];

ll z;
basic_string<pair<int, int>> sol;

void dfs_brate(int x, int p) {
	for (int y : e[x]) {
		if (y != p) {
			dfs_brate(y, x);
		}
	}
	if (!on_diam[x]) {
		if (d1[x] > d2[x]) {
			z += d1[x];
			sol += {x, f};
		} else {
			z += d2[x];
			sol += {x, g};
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.tie(nullptr);

	cin >> n;
	for (int i=1; i<n; i++) {
		int u, v;
		cin >> u >> v;
		e[u] += v;
		e[v] += u;
	}

	bfs(1, d1);
	f = max_element(d1+1, d1+n+1) - d1;
	bfs(f, d1);
	g = max_element(d1+1, d1+n+1) - d1;
	int diam = d1[g];
	h = g;
	basic_string<int> dnodes;
	for (int i=0; i<diam; i++) {
		on_diam[h] = 1;
		dnodes += h;
		h = p[h];
	}
	on_diam[f] = 1;
	bfs(g, d2);

	dfs_brate(f, f);

	for (int i=0; i<diam; i++) {
		z += diam - i;
		sol += {f, dnodes[i]};
	}

	cout << z << '\n';
	for (auto [x, y] : sol)
		cout << x << ' ' << y << ' ' << x << '\n';
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define maxn 200000+5
#define inf 0x3f3f3f3f
#define eps 1e-8

typedef long long ll;

using namespace std;

int dep[maxn];
int fa[maxn];
int vis[maxn];
ll sum=0;

struct node
{
	int l,r,cut;
};

vector<node>ans;
vector<int>edge[maxn];
int l,r;

void dfs(int now,int pre)
{
	for(int i=0;i<(int)edge[now].size();i++)
	{
		int v=edge[now][i];
		if(v==pre) continue;
		dep[v]=dep[now]+1;
		fa[v]=now;
		dfs(v,now);
	}
}

void dfs2(int now,int pre,int d)
{
	if(vis[now]) d++;
	for(int i=0;i<(int)edge[now].size();i++)
	{
		int v=edge[now][i];
		if(v==pre) continue;
		dfs2(v,now,d);
		if(!vis[v])//留着直径
		{
			int tmp1=dep[v];
			int tmp2=dep[v]+dep[r]-2*d;
			if(tmp1>tmp2)
			{
				sum+=tmp1;
				ans.push_back((node){l,v,v});
			}else
			{
				sum+=tmp2;
				ans.push_back((node){r,v,v});
			}
		}
	}
}

int main()
{
	int n;
	scanf("%d",&n);
	int u,v;
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&u,&v);
		edge[u].push_back(v);
		edge[v].push_back(u);
	}
	//找直径
	dep[1]=fa[1]=0;
	dfs(1,0);
	l=1;
	for(int i=2;i<=n;i++)
	{
		if(dep[i]>dep[l]) l=i;
	}
	fa[l]=dep[l]=0;
	dfs(l,0);
	r=l;
	for(int i=1;i<=n;i++)
	{
		if(dep[i]>dep[r]) r=i;
	}
	for(int i=r;i!=l;i=fa[i]) vis[i]=1;
	dfs2(l,0,0);
	for(int i=r;i!=l;i=fa[i])
	{
		sum+=dep[i];
		ans.push_back((node){l,i,i});
	}
	printf("%lld\n",sum);
	for(int i=0;i<(int)ans.size();i++)
	{
		printf("%d %d %d\n",ans[i].l,ans[i].r,ans[i].cut);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define maxn 200000+5
#define inf 0x3f3f3f3f
#define eps 1e-8
#define pii pair<int,int>

typedef long long ll;

using namespace std;

int dep[maxn];
int fa[maxn];
int vis[maxn];
ll sum=0;
vector<pair<int,pair<int,int> > >ans;
vector<int>edge[maxn];
int l,r;

void dfs(int now,int pre)
{
	for(int i=0;i<(int)edge[now].size();i++)
	{
		int v=edge[now][i];
		if(v==pre) continue;
		dep[v]=dep[now]+1;
		fa[v]=now;
		dfs(v,now);
	}
}

void dfs2(int now,int pre,int d)
{
	if(vis[now]) d++;
	for(int i=0;i<(int)edge[now].size();i++)
	{
		int v=edge[now][i];
		if(v==pre) continue;
		dfs2(v,now,d);
		if(!vis[v])
		{
			int tmp1=dep[v];
			int tmp2=dep[v]+dep[r]-2*d;
			pair<int,int>tmp3;
			tmp3.first=v;
			tmp3.second=v;
			if(tmp1>tmp2)
			{
				sum+=tmp1;

				ans.push_back(make_pair(l,tmp3));
			}else
			{
				sum+=tmp2;
				ans.push_back(make_pair(r,tmp3));
			}
		}
	}
}

int main()
{
	int n;
	scanf("%d",&n);
	int u,v;
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&u,&v);
		edge[u].push_back(v);
		edge[v].push_back(u);
	}
	//找直径
	dep[1]=fa[1]=0;
	dfs(1,0);
	l=1;
	for(int i=2;i<=n;i++)
	{
		if(dep[i]>dep[l]) l=i;
	}
	fa[l]=dep[l]=0;
	dfs(l,0);
	r=l;
	for(int i=1;i<=n;i++)
	{
		if(dep[i]>dep[r]) r=i;
	}
	for(int i=r;i!=l;i=fa[i]) vis[i]=1;
	dfs2(l,0,0);
	pair<int,int>tmp;
	for(int i=r;i!=l;i=fa[i])
	{
		sum+=dep[i];
		tmp.first=i;
		tmp.second=i;
		ans.push_back(make_pair(l,tmp));
	}
	printf("%lld\n",sum);
	for(int i=0;i<(int)ans.size();i++)
	{
		printf("%d %d %d\n",ans[i].first,ans[i].second.first,ans[i].second.second);
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
int n,head[200009],tot;
struct pp{int nxt,to;}g[400009];
void add(int u,int v){g[++tot].nxt=head[u],g[tot].to=v,head[u]=tot;}
int t[200009][2],w[200009][2];
int f[200009][21];
ll d[200009];
void dfs(int u,int fa){
	d[u]=d[fa]+1,f[u][0]=fa;
	w[u][0]=u,w[u][1]=u;
	for(int i=head[u];i!=-1;i=g[i].nxt){
		int v=g[i].to;
		if(v==fa) continue;
		dfs(v,u);
		if(t[v][0]+1>t[u][0]) t[u][1]=t[u][0],t[u][0]=t[v][0]+1,w[u][1]=w[u][0],w[u][0]=w[v][0];
		else if(t[v][0]+1>t[u][1]) t[u][1]=t[v][0]+1,w[u][1]=w[v][0];
	}
}
int lca(int a,int b){
	if(d[a]<d[b]) swap(a,b);
	for(int i=20;i>=0;i--)
		if(d[f[a][i]]>=d[b]) a=f[a][i];
	if(a==b) return a;
	for(int i=20;i>=0;i--)
		if(f[a][i]!=f[b][i]) a=f[a][i],b=f[b][i];
	return f[a][0];
}
ll dis(int a,int b){
	return d[a]+d[b]-2*d[lca(a,b)];
}
int x,y,ansx[200009],ansy[200009],ansc[200009],top;
ll ans;
int vis[200009];
void flag(int u,int v){
	while(u!=v){
		vis[u]=1;
		u=f[u][0];
	}
	vis[v]=1;
	return ;
}
void Dfs(int u,int fa){
	for(int i=head[u];i!=-1;i=g[i].nxt){
		int v=g[i].to;
		if(v==fa) continue;
		Dfs(v,u);
	}
	if(!vis[u]){
		if(dis(u,x)>dis(u,y)){
			ansx[++top]=u,ansy[top]=x,ansc[top]=u,ans+=dis(u,x);
		}
		else{
			ansx[++top]=u,ansy[top]=y,ansc[top]=u,ans+=dis(u,y);
		}
	}
}

int main(){
	memset(f,0,sizeof(f));
	memset(head,-1,sizeof(head));tot=0;
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		add(u,v);add(v,u);
	}
	d[1]=0;
	dfs(1,1);
	int sum=0;
	for(int i=1;i<=n;i++){
		if(t[i][0]+t[i][1]>sum) sum=t[i][0]+t[i][1],x=w[i][0],y=w[i][1];
	}
	for(int j=1;j<=20;j++){
		for(int i=1;i<=n;i++) f[i][j]=f[f[i][j-1]][j-1];
	}
	memset(vis,0,sizeof(vis));
	int p=lca(x,y);
	top=0,ans=0;
	flag(x,p);flag(y,p);
	Dfs(p,p);
	while(x!=p){
		ansx[++top]=x,ansy[top]=y,ansc[top]=x,ans+=dis(x,y);
		x=f[x][0];
	}
	while(y!=p){
		ansx[++top]=x,ansy[top]=y,ansc[top]=y,ans+=dis(x,y);
		y=f[y][0];
	}
	printf("%lld\n",ans);
	for(int i=1;i<=top;i++) printf("%d %d %d\n",ansx[i],ansy[i],ansc[i]);
	return 0;
}
/*ÿ��̰�ĵı���ֱ��һ�����ţ�ÿ���㵽ֱ�����˵���Զ��һ�����
֤������֤�������ǰһ�㵽ֱ�����˵���Զ�㲻�����
��ô1.�������ֱ�������k���ţ���õ㵽ֱ�����˵���Զ����ȣ��ཻ��ֱ��
�ĵ㵽k��·��һ�������ཻ�㵽ֱ����Զ��ĳ��ȡ���ôk��ֱ����һ��ľ��뽫
����ֱ����������ֱ�����������·������
2.������ֱ����ֱ����ֱ��������ֱ��*/


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
int n,head[200009],tot;
struct pp{int nxt,to;}g[400009];
void add(int u,int v){g[++tot].nxt=head[u],g[tot].to=v,head[u]=tot;}
int t[200009][2],w[200009][2];
int f[200009][21],d[200009];
void dfs(int u,int fa){
	d[u]=d[fa]+1,f[u][0]=fa;
	w[u][0]=u,w[u][1]=u;
	for(int i=head[u];i!=-1;i=g[i].nxt){
		int v=g[i].to;
		if(v==fa) continue;
		dfs(v,u);
		if(t[v][0]+1>t[u][0]) t[u][1]=t[u][0],t[u][0]=t[v][0]+1,w[u][1]=w[u][0],w[u][0]=w[v][0];
		else if(t[v][0]+1>t[u][1]) t[u][1]=t[v][0]+1,w[u][1]=w[v][0];
	}
}
int lca(int a,int b){
	if(d[a]<d[b]) swap(a,b);
	for(int i=20;i>=0;i--)
		if(d[f[a][i]]>=d[b]) a=f[a][i];
	if(a==b) return a;
	for(int i=20;i>=0;i--)
		if(f[a][i]!=f[b][i]) a=f[a][i],b=f[b][i];
	return f[a][0];
}
int dis(int a,int b){
	return d[a]+d[b]-2*d[lca(a,b)];
}
int x,y,ans,ansx[200009],ansy[200009],ansc[200009],top;
int vis[200009];
void flag(int u,int v){
	while(u!=v){
		vis[u]=1;
		u=f[u][0];
	}
	vis[v]=1;
	return ;
}
void Dfs(int u,int fa){
	for(int i=head[u];i!=-1;i=g[i].nxt){
		int v=g[i].to;
		if(v==fa) continue;
		Dfs(v,u);
	}
	if(!vis[u]){
		if(dis(u,x)>dis(u,y)){
			ansx[++top]=u,ansy[top]=x,ansc[top]=u,ans+=dis(u,x);
		}
		else{
			ansx[++top]=u,ansy[top]=y,ansc[top]=u,ans+=dis(u,y);
		}
	}
}

int main(){
	memset(f,0,sizeof(f));
	memset(head,-1,sizeof(head));tot=0;
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		add(u,v);add(v,u);
	}
	d[1]=0;
	dfs(1,1);
	int sum=0;
	for(int i=1;i<=n;i++){
		if(t[i][0]+t[i][1]>sum) sum=t[i][0]+t[i][1],x=w[i][0],y=w[i][1];
	}
	for(int j=1;j<=20;j++){
		for(int i=1;i<=n;i++) f[i][j]=f[f[i][j-1]][j-1];
	}
	memset(vis,0,sizeof(vis));
	int p=lca(x,y);
	top=0,ans=0;
	flag(x,p);flag(y,p);
	Dfs(p,p);
	while(x!=p){
		ansx[++top]=x,ansy[top]=y,ansc[top]=x,ans+=dis(x,y);
		x=f[x][0];
	}
	while(y!=p){
		ansx[++top]=x,ansy[top]=y,ansc[top]=y,ans+=dis(x,y);
		y=f[y][0];
	}
	printf("%d\n",ans);
	for(int i=1;i<=top;i++) printf("%d %d %d\n",ansx[i],ansy[i],ansc[i]);
	return 0;
}
/*ÿ��̰�ĵı���ֱ��һ�����ţ�ÿ���㵽ֱ�����˵���Զ��һ�����
֤������֤�������ǰһ�㵽ֱ�����˵���Զ�㲻�����
��ô1.�������ֱ�������k���ţ���õ㵽ֱ�����˵���Զ����ȣ��ཻ��ֱ��
�ĵ㵽k��·��һ�������ཻ�㵽ֱ����Զ��ĳ��ȡ���ôk��ֱ����һ��ľ��뽫
����ֱ����������ֱ�����������·������
2.������ֱ����ֱ����ֱ��������ֱ��*/


#include<bits/stdc++.h>
using namespace std;
#define Ma 0x3f3f3f3f;
int const m1=1e4+2;
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<3;i++)
    {
        int x,y;
        cin>>x>>y;
        if(n==x)
            n=y;
        if(n==y)
            n=x;
    }
    cout<<n<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define Ma 0x3f3f3f3f;
int const m1=1e4+2;
int main()
{
    int a[4],n;
    a[3]=a[1]=a[2]=0;
    cin>>n;
    a[n]=1;
    for(int i=1;i<=3;i++)
    {
        int x,y;
        cin>>x>>y;
        swap(a[x],a[y]);
    }
    for(int i=1;i<=3;i++)
        if(a[i])cout<<i;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define Ma 0x3f3f3f3f;
int const m1=1e4+2;
int main()
{
    int n,a[1002];
    memset(a,0,sizeof(a));
    cin>>n;
    a[n]=1;
    for(int i=1;i<=3;i++)
    {
        int x,y,z;
        cin>>x>>y;
        z=a[x];
        a[x]=a[y];
        a[y]=z;
    }
    for(int i=1;i<=3;i++)
    {
        if(a[i])cout<<i<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define Ma 0x3f3f3f3f;
int const m1=1e4+2;
int main()
{
    int n,a[1002];
    memset(a,0,sizeof(a));
    cin>>n;
    a[n]=1;
    for(int i=1;i<=3;i++)
    {
        int x,y;
        cin>>x>>y;
        swap(a[x],a[y]);
    }
    for(int i=1;i<=3;i++)
    {
        if(a[i])cout<<i<<endl;
    }
    return 0;
}


#include<fstream>
using namespace std;
int n,x,y;
ifstream cin("input.txt");
ofstream cout("output.txt");
int main()
{
	cin>>n;
	for(int i=0;i<3;i++){
		cin>>x>>y;
		n=(n==x)?y:(n==y)?x:n;
	}
	cout<<n;
	return 0;
}


#include<fstream>
using namespace std;
#define Ma 0x3f3f3f3f;
int const m1=1e4+2;
int n;
ifstream cin("input.txt");
ofstream cout("output.txt");
int main()
{
    cin>>n;
    for(int i=0;i<3;i++)
    {
        int x,y;
        cin>>x>>y;
        if(n==x)
            n=y;
        if(n==y)
            n=x;
    }
    cout<<n<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define Ma 0x3f3f3f3f;
int const m1=1e4+2;
int main()
{
    int n,a[1002];
    memset(a,0,sizeof(a));
    cin>>n;
    a[n]=1;
    for(int i=0;i<3;i++)
    {
        int x,y;
        cin>>x>>y;
        swap(a[x],a[y]);
    }
    for(int i=1;i<=3;i++)
    {
        if(a[i])cout<<i<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
double ac;
double TLE() {return (double)(clock() - ac) / CLOCKS_PER_SEC;}
//only try
const int N =1000000;
int f[N];
int main(){
 std::ios::sync_with_stdio(0);
 int k;
 cin>>k;
 int y = k;
 f[y] = 1;
 for(int i=0;i<3;i++){
 	int x,y;
 	cin>>x>>y;
 	if(f[x]==1){
 		f[y] = 1;
 		f[x] = 0;
 	}
 	else if(f[y]==1){
 		f[x]=1;
 		f[y]=0;
 	}
 }
 int m=0;
 for(int i=0;i<4;i++){
 	if(f[i]==1){
 		m = i;
 		break;
 	}
 }
 cout<<m<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
double ac;
double TLE() {return (double)(clock() - ac) / CLOCKS_PER_SEC;}
//only try

int main(){
 std::ios::sync_with_stdio(0);
 int k;
 cin>>k;
 map<int,int> m;
 m[k]=1;
 for(int i=0;i<3;i++){
 	int x,y;
 	cin>>x>>y;
 	if(m[x]==1){
 		m[y] = 1;
 		m[x] = 0;
 	}
 	else if(m[y]==1){
 		m[x]=1;
 		m[y]=0;
 	}
 }
 int d=0;
 for(int i=0;i<4;i++){
 	if(m[i]==1){
 		d = i;
 		break;
 	}
 }
 cout<<d<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
double ac;
double TLE() {return (double)(clock() - ac) / CLOCKS_PER_SEC;}
//only try

int main(){
 std::ios::sync_with_stdio(0);
 freopen("input.txt", "r", stdin);
 freopen("output.txt", "w", stdout);
 int k;
 cin>>k;
 map<int,int> m;
 m[k]=1;
 for(int i=0;i<3;i++){
 	int x,y;
 	cin>>x>>y;
 	if(m[x]==1){
 		m[y] = 1;
 		m[x] = 0;
 	}
 	else if(m[y]==1){
 		m[x]=1;
 		m[y]=0;
 	}
 }
 int d=0;
 for(int i=0;i<4;i++){
 	if(m[i]==1){
 		d = i;
 		break;
 	}
 }
 cout<<d<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
double ac;
double TLE() {return (double)(clock() - ac) / CLOCKS_PER_SEC;}
//only try

int main(){
 std::ios::sync_with_stdio(0);
 int f[4] = {0};
 int k;
 cin>>k;
 f[k] = 1;
 for(int i=0;i<3;i++){
 	int x,y;
 	cin>>x>>y;
 	if(f[x]==1){
 		f[y] = 1;
 		f[x] = 0;
 	}
 	else if(f[y]==1){
 		f[x]=1;
 		f[y]=0;
 	}
 }
 int m=0;
 for(int i=0;i<4;i++){
 	if(f[i]==1){
 		m = i;
 		break;
 	}
 }
 cout<<m<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
double ac;
double TLE() {return (double)(clock() - ac) / CLOCKS_PER_SEC;}
//only try
const int N =1000000;
int main(){
 std::ios::sync_with_stdio(0);
 int f[N] = {0};
 int k;
 cin>>k;
 f[k] = 1;
 for(int i=0;i<3;i++){
 	int x,y;
 	cin>>x>>y;
 	if(f[x]==1){
 		f[y] = 1;
 		f[x] = 0;
 	}
 	else if(f[y]==1){
 		f[x]=1;
 		f[y]=0;
 	}
 }
 int m=0;
 for(int i=0;i<4;i++){
 	if(f[i]==1){
 		m = i;
 		break;
 	}
 }
 cout<<m<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
double ac;
double TLE() {return (double)(clock() - ac) / CLOCKS_PER_SEC;}
//only try

int main(){
 std::ios::sync_with_stdio(0);
 bool f[4] = {false};
 int k;
 cin>>k;
 f[k] = true;
 for(int i=0;i<3;i++){
 	int x,y;
 	cin>>x>>y;
 	if(f[x]==true){
 		f[y] = true;
 		f[x] = false;
 	}
 	else if(f[y]==true){
 		f[x]=true;
 		f[y]=false;
 	}
 }
 int m=0;
 for(int i=0;i<4;i++){
 	if(f[i]==true){
 		m = i;
 		break;
 	}
 }
 cout<<m<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
double ac;
double TLE() {return (double)(clock() - ac) / CLOCKS_PER_SEC;}
//only try

int main(){
 std::ios::sync_with_stdio(0);
 freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
 bool f[4] = {false};
 int k;
 cin>>k;
 f[k] = true;
 for(int i=0;i<3;i++){
 	int x,y;
 	cin>>x>>y;
 	if(f[x]==true){
 		f[y] = true;
 		f[x] = false;
 	}
 	else if(f[y]==true){
 		f[x]=true;
 		f[y]=false;
 	}
 }
 int m=0;
 for(int i=0;i<4;i++){
 	if(f[i]==true){
 		m = i;
 		break;
 	}
 }
 cout<<m<<endl;

}


#include <bits/stdc++.h>
#define int long long
#define intu unsigned long long
#define elif else if
#define pb push_back
#define mp make_pair
#define ln '\n'
#define fo(i,n)  for(int i=0;i<n;i++)
#define forr(i,b,n) for(int i=b;i<=n;i++)
#define forrv(i,b,n) for(int i=b;i>=n;i--)
#define fors(i,x) for(auto i=x.begin(); i!=x.end(); i++)
#define all(x) x.begin(),x.end()
#define pqmin(x) priority_queue<x, vector<x>, greater<x> >
#define pqmax(x) priority_queue<x>
#define sortdsc(x) sort(aint(x),greater<int>())
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define mpii map<int,int>
#define pi 3.14159265358979323846D
#define gcd(x,y) __gcd(x,y)
#define lcm(x,y) (x*(y/__gcd(x,y)))
#define arrin(a,n) fo(i,n) cin >> a[i];
#define arrout(a,n) fo(i,n) {cout << a[i] << " ";} cout << ln;

using namespace std;
// Shell Game
int32_t main()
{
    fast;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int ans;
    cin>>ans;

    vi x,y;

    fo(i,3)
    {
        int k;
        cin>>k;
        x.pb(k);
        cin>>k;
        y.pb(k);
    }

    fo(i,3)
    {

        int xx=x[i];
        int yy=y[i];

        if(xx==ans)
        {
            ans=yy;
        }
        if(yy==ans)
        {
            ans=xx;
        }
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define intu unsigned long long
#define elif else if
#define pb push_back
#define mp make_pair
#define ln '\n'
#define fo(i,n)  for(int i=0;i<n;i++)
#define forr(i,b,n) for(int i=b;i<=n;i++)
#define forrv(i,b,n) for(int i=b;i>=n;i--)
#define fors(i,x) for(auto i=x.begin(); i!=x.end(); i++)
#define all(x) x.begin(),x.end()
#define pqmin(x) priority_queue<x, vector<x>, greater<x> >
#define pqmax(x) priority_queue<x>
#define sortdsc(x) sort(aint(x),greater<int>())
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define mpii map<int,int>
#define pi 3.14159265358979323846D
#define gcd(x,y) __gcd(x,y)
#define lcm(x,y) (x*(y/__gcd(x,y)))
#define arrin(a,n) fo(i,n) cin >> a[i];
#define arrout(a,n) fo(i,n) {cout << a[i] << " ";} cout << ln;

using namespace std;
// Shell Game
int32_t main()
{
    fast;
    // #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    // #endif
    int ans;
    cin>>ans;

    vi x,y;

    fo(i,3)
    {
        int k;
        cin>>k;
        x.pb(k);
        cin>>k;
        y.pb(k);
    }

    fo(i,3)
    {

        int xx=x[i];
        int yy=y[i];

        if(xx==ans)
        {
            ans=yy;
        }
        if(yy==ans)
        {
            ans=xx;
        }
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define intu unsigned long long
#define elif else if
#define pb push_back
#define mp make_pair
#define ln '\n'
#define fo(i,n)  for(int i=0;i<n;i++)
#define forr(i,b,n) for(int i=b;i<=n;i++)
#define forrv(i,b,n) for(int i=b;i>=n;i--)
#define fors(i,x) for(auto i=x.begin(); i!=x.end(); i++)
#define all(x) x.begin(),x.end()
#define pqmin(x) priority_queue<x, vector<x>, greater<x> >
#define pqmax(x) priority_queue<x>
#define sortdsc(x) sort(aint(x),greater<int>())
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define mpii map<int,int>
#define pi 3.14159265358979323846D
#define gcd(x,y) __gcd(x,y)
#define lcm(x,y) (x*(y/__gcd(x,y)))
#define arrin(a,n) fo(i,n) cin >> a[i];
#define arrout(a,n) fo(i,n) {cout << a[i] << " ";} cout << ln;

using namespace std;
// Shell Game
int32_t main()
{
    fast;
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif
    int ans;
    cin>>ans;

    vi x,y;

    fo(i,3)
    {
        int k;
        cin>>k;
        x.pb(k);
        cin>>k;
        y.pb(k);
    }

    fo(i,3)
    {

        int xx=x[i];
        int yy=y[i];

        if(xx==ans)
        {
            ans=yy;
        }
        if(yy==ans)
        {
            ans=xx;
        }
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define intu unsigned long long
#define elif else if
#define pb push_back
#define mp make_pair
#define ln '\n'
#define fo(i,n)  for(int i=0;i<n;i++)
#define forr(i,b,n) for(int i=b;i<=n;i++)
#define forrv(i,b,n) for(int i=b;i>=n;i--)
#define fors(i,x) for(auto i=x.begin(); i!=x.end(); i++)
#define all(x) x.begin(),x.end()
#define pqmin(x) priority_queue<x, vector<x>, greater<x> >
#define pqmax(x) priority_queue<x>
#define sortdsc(x) sort(aint(x),greater<int>())
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define mpii map<int,int>
#define pi 3.14159265358979323846D
#define gcd(x,y) __gcd(x,y)
#define lcm(x,y) (x*(y/__gcd(x,y)))
#define arrin(a,n) fo(i,n) cin >> a[i];
#define arrout(a,n) fo(i,n) {cout << a[i] << " ";} cout << ln;

using namespace std;
// Shell Game
int32_t main()
{
    fast;
    // #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    // #endif
    int ans;
    cin>>ans;

    vi x,y;

    fo(i,3)
    {
        int k;
        cin>>k;
        x.pb(k);
        cin>>k;
        y.pb(k);
    }

    fo(i,3)
    {

        int xx=x[i];
        int yy=y[i];

        if(xx==ans)
        {
            ans=yy;
        }
        else if(yy==ans)
        {
            ans=xx;
        }

        // cout<<ans<<ln;
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define intu unsigned long long
#define elif else if
#define pb push_back
#define mp make_pair
#define ln '\n'
#define fo(i,n)  for(int i=0;i<n;i++)
#define forr(i,b,n) for(int i=b;i<=n;i++)
#define forrv(i,b,n) for(int i=b;i>=n;i--)
#define fors(i,x) for(auto i=x.begin(); i!=x.end(); i++)
#define all(x) x.begin(),x.end()
#define pqmin(x) priority_queue<x, vector<x>, greater<x> >
#define pqmax(x) priority_queue<x>
#define sortdsc(x) sort(aint(x),greater<int>())
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define mpii map<int,int>
#define pi 3.14159265358979323846D
#define gcd(x,y) __gcd(x,y)
#define lcm(x,y) (x*(y/__gcd(x,y)))
#define arrin(a,n) fo(i,n) cin >> a[i];
#define arrout(a,n) fo(i,n) {cout << a[i] << " ";} cout << ln;

using namespace std;
// Shell Game
int32_t main()
{
    fast;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int ans;
    cin>>ans;

    int x,y;

    fo(i,3)
    {
        cin>>x>>y;

        int xx=x;
        int yy=y;

        if(xx==ans)
        {
            ans=yy;
        }
        if(yy==ans)
        {
            ans=xx;
        }
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define intu unsigned long long
#define elif else if
#define pb push_back
#define mp make_pair
#define ln '\n'
#define fo(i,n)  for(int i=0;i<n;i++)
#define forr(i,b,n) for(int i=b;i<=n;i++)
#define forrv(i,b,n) for(int i=b;i>=n;i--)
#define fors(i,x) for(auto i=x.begin(); i!=x.end(); i++)
#define all(x) x.begin(),x.end()
#define pqmin(x) priority_queue<x, vector<x>, greater<x> >
#define pqmax(x) priority_queue<x>
#define sortdsc(x) sort(aint(x),greater<int>())
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define mpii map<int,int>
#define pi 3.14159265358979323846D
#define gcd(x,y) __gcd(x,y)
#define lcm(x,y) (x*(y/__gcd(x,y)))
#define arrin(a,n) fo(i,n) cin >> a[i];
#define arrout(a,n) fo(i,n) {cout << a[i] << " ";} cout << ln;

using namespace std;
// Shell Game
int32_t main()
{
    fast;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int ans;
    cin>>ans;

    int x,y;

    fo(i,3)
    {
    cin>>x>>y;

    if(x==ans)
    {
        ans=y;
    }
    if(y==ans)
    {
        ans=x;
    }}
    cout<<ans;

    return 0;
}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstdio>
#include<sstream>
#include<utility>
#include<string>
#include<cstdlib>
#include<valarray>
#include<algorithm>
#include<vector>
#include<iterator>
#include<list>
#include<set>
#include<numeric>
#include<bits/stdc++.h>
#define ll long long
ll prime[100001];
using namespace std;
int Prime(int a)
{
    for(int i=2;i<=a/2;++i)
    {
        if(a%i==0)
        {
            return 0;
        }
    }
    return 1;
}
void sieve()
{   for(int i=0;i<=100000;i++)
    prime[i]=0;

    int maxn=100000;
    for(int i=2;i<=maxn;i++)
    {
        if(prime[i]==0)
        {
            for(int j=i;j<=maxn;j+=i)
            {
                prime[j]++;
            }
        }
    }
}
void fibo(ll a[],ll n)
{
    ll x=0;
    ll y=1;
    a[0]=0;
    a[1]=1;
    for(ll i=2;i<n;++i)
    {
        a[i]=(x+y)%10;
        x=y;
        y=a[i];
    }
}
ll f(ll w[],ll a[][2],ll i,ll y)
{
    for(ll j=0;j<y;++j)
    {
        if(a[i][2]<=w[j])
        {
            cout<<a[i][2];
            return w[j];
        }
    }
    return w[y-1];
}
ll s(ll v[],ll a[][2],ll i,ll x)
{
    for(ll j=0;j<x;++j)
    {
        if(a[i][1]>=v[j])
        {
            cout<<" "<<a[i][1]<<" ";
            return v[j];
        }
    }
    return v[0];
}
int main()
{
    cin.tie(NULL);
	ios_base::sync_with_stdio(false);
	freopen("input.txt","r/w",stdin/stdout);
    int x,a,b;
    cin>>x;
    for(int i=0;i<3;++i)
    {
    	cin>>a>>b;
    	if(a==x)
    	{
    		x=b;
		}
		else if(b==x)
		{
			x=a;
		}
	}
	cout<<x;
    return 0;
}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstdio>
#include<sstream>
#include<utility>
#include<string>
#include<cstdlib>
#include<valarray>
#include<algorithm>
#include<vector>
#include<iterator>
#include<list>
#include<set>
#include<numeric>
#include<bits/stdc++.h>
#define ll long long
ll prime[100001];
using namespace std;
int Prime(int a)
{
    for(int i=2;i<=a/2;++i)
    {
        if(a%i==0)
        {
            return 0;
        }
    }
    return 1;
}
void sieve()
{   for(int i=0;i<=100000;i++)
    prime[i]=0;

    int maxn=100000;
    for(int i=2;i<=maxn;i++)
    {
        if(prime[i]==0)
        {
            for(int j=i;j<=maxn;j+=i)
            {
                prime[j]++;
            }
        }
    }
}
void fibo(ll a[],ll n)
{
    ll x=0;
    ll y=1;
    a[0]=0;
    a[1]=1;
    for(ll i=2;i<n;++i)
    {
        a[i]=(x+y)%10;
        x=y;
        y=a[i];
    }
}
ll f(ll w[],ll a[][2],ll i,ll y)
{
    for(ll j=0;j<y;++j)
    {
        if(a[i][2]<=w[j])
        {
            cout<<a[i][2];
            return w[j];
        }
    }
    return w[y-1];
}
ll s(ll v[],ll a[][2],ll i,ll x)
{
    for(ll j=0;j<x;++j)
    {
        if(a[i][1]>=v[j])
        {
            cout<<" "<<a[i][1]<<" ";
            return v[j];
        }
    }
    return v[0];
}
int main()
{
    cin.tie(NULL);
	ios_base::sync_with_stdio(false);
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
    int x,a,b;
    cin>>x;
    for(int i=0;i<3;++i)
    {
    	cin>>a>>b;
    	if(a==x)
    	{
    		x=b;
		}
		else if(b==x)
		{
			x=a;
		}
	}
	cout<<x;
    return 0;
}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstdio>
#include<sstream>
#include<utility>
#include<string>
#include<cstdlib>
#include<valarray>
#include<algorithm>
#include<vector>
#include<iterator>
#include<list>
#include<set>
#include<numeric>
#include<bits/stdc++.h>
#define ll long long
ll prime[100001];
using namespace std;
int Prime(int a)
{
    for(int i=2;i<=a/2;++i)
    {
        if(a%i==0)
        {
            return 0;
        }
    }
    return 1;
}
void sieve()
{   for(int i=0;i<=100000;i++)
    prime[i]=0;

    int maxn=100000;
    for(int i=2;i<=maxn;i++)
    {
        if(prime[i]==0)
        {
            for(int j=i;j<=maxn;j+=i)
            {
                prime[j]++;
            }
        }
    }
}
void fibo(ll a[],ll n)
{
    ll x=0;
    ll y=1;
    a[0]=0;
    a[1]=1;
    for(ll i=2;i<n;++i)
    {
        a[i]=(x+y)%10;
        x=y;
        y=a[i];
    }
}
ll f(ll w[],ll a[][2],ll i,ll y)
{
    for(ll j=0;j<y;++j)
    {
        if(a[i][2]<=w[j])
        {
            cout<<a[i][2];
            return w[j];
        }
    }
    return w[y-1];
}
ll s(ll v[],ll a[][2],ll i,ll x)
{
    for(ll j=0;j<x;++j)
    {
        if(a[i][1]>=v[j])
        {
            cout<<" "<<a[i][1]<<" ";
            return v[j];
        }
    }
    return v[0];
}
int main()
{
    ll x,a,b;
    cin>>x;
    for(ll i=0;i<3;++i)
    {
    	cin>>a>>b;
    	if(a==x)
    	{
    		x=b;
		}
		else if(b==x)
		{
			x=a;
		}
	}
	cout<<x;
    return 0;
}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstdio>
#include<sstream>
#include<utility>
#include<string>
#include<cstdlib>
#include<valarray>
#include<algorithm>
#include<vector>
#include<iterator>
#include<list>
#include<set>
#include<numeric>
#include<bits/stdc++.h>
#define ll long long
ll prime[100001];
using namespace std;
int Prime(int a)
{
    for(int i=2;i<=a/2;++i)
    {
        if(a%i==0)
        {
            return 0;
        }
    }
    return 1;
}
void sieve()
{   for(int i=0;i<=100000;i++)
    prime[i]=0;

    int maxn=100000;
    for(int i=2;i<=maxn;i++)
    {
        if(prime[i]==0)
        {
            for(int j=i;j<=maxn;j+=i)
            {
                prime[j]++;
            }
        }
    }
}
void fibo(ll a[],ll n)
{
    ll x=0;
    ll y=1;
    a[0]=0;
    a[1]=1;
    for(ll i=2;i<n;++i)
    {
        a[i]=(x+y)%10;
        x=y;
        y=a[i];
    }
}
ll f(ll w[],ll a[][2],ll i,ll y)
{
    for(ll j=0;j<y;++j)
    {
        if(a[i][2]<=w[j])
        {
            cout<<a[i][2];
            return w[j];
        }
    }
    return w[y-1];
}
ll s(ll v[],ll a[][2],ll i,ll x)
{
    for(ll j=0;j<x;++j)
    {
        if(a[i][1]>=v[j])
        {
            cout<<" "<<a[i][1]<<" ";
            return v[j];
        }
    }
    return v[0];
}
int main()
{
    int x,a,b;
    cin>>x;
    for(int i=0;i<3;++i)
    {
    	cin>>a>>b;
    	if(a==x)
    	{
    		x=b;
		}
		else if(b==x)
		{
			x=a;
		}
	}
	cout<<x;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10000;

int a,n,m,b[N],k;
vector<int>v;
map<int,int>mp;
int main(){
	freopen("input.txt","output.txt","w");
	for(int i=0;i<3;i++){
		cin>>m>>k;
		if(m==n)n=k;
		if(k==n)n=m;
	}
	cout<<n<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10000;

int a,n,m,b[N],k;
vector<int>v;
map<int,int>mp;
int main(){
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	for(int i=0;i<3;i++){
		if(m==n)n=k;
		if(k==n)n=m;
	}
//	cout<<n<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10000;

int a,n,m,b[N],k;
vector<int>v;
map<int,int>mp;
int main(){
	freopen("input.txt","r",stdin);
	for(int i=0;i<3;i++){
		cin>>m>>k;
		if(m==n)n=k;
		if(k==n)n=m;
	}
	freopen("output.txt","w",stdout);
//	cout<<n<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10000;

int a,n,m,b[N],k;
vector<int>v;
map<int,int>mp;
int main(){
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	for(int i=0;i<3;i++){
		if(m==n)n=k;
		if(k==n)n=m;
	}
	cout<<n<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10000;

int a,n,m,b[N],k;
vector<int>v;
map<int,int>mp;
int main(){
	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);
	for(int i=0;i<3;i++){
		if(m==n)n=k;
		if(k==n)n=m;
	}
//	cout<<n<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10000;

int a,n,m,b[N],k;
vector<int>v;
map<int,int>mp;
int main(){
	freopen("in.txt","r",stdin);
	for(int i=0;i<3;i++){
		cin>>m>>k;
		if(m==n)n=k;
		if(k==n)n=m;
	}
	freopen("out.txt","w",stdout);
//	cout<<n<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10000;

int a,n,m,b[N],k;
vector<int>v;
map<int,int>mp;
int main(){
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);
	for(int i=0;i<3;i++){
		if(m==n)n=k;
		if(k==n)n=m;
	}
//	cout<<n<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10000;

int a,n,m,b[N],k;
vector<int>v;
map<int,int>mp;
int main(){
	cin>>n;
	for(int i=0;i<3;i++){
		cin>>m>>k;
		if(m==n)n=k;
		if(k==n)n=m;
	}
	cout<<n<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10000;

int a,n,m,b[N],k;
int main(){
	cin>>n;
	for(int i=0;i<3;i++){
		cin>>m>>k;
		if(m==n)n=k;
		if(k==n)n=m;
	}
	cout<<n<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10000;

int a,n,m,b[N],k;
vector<int>v;
map<int,int>mp;
int main(){
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	cin>>n;
	for(int i=0;i<3;i++){
		cin>>m>>k;
		if(m==n)n=k;
		else if(k==n)n=m;
	}
	cout<<k<<n<<m<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10000;

int a,n,m,b[N],k;
vector<int>v;
map<int,int>mp;
int main(){
	freopen("input.txt","r",stdin);
	for(int i=0;i<3;i++){
		if(m==n)n=k;
		if(k==n)n=m;
	}
	freopen("output.txt","w",stdout);
//	cout<<n<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =10000;

int a,n,m,b[N],k;
vector<int>v;
map<int,int>mp;
int main(){
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	cin>>n;
	for(int i=0;i<3;i++){
		cin>>m>>k;
		if(m==n)n=k;
		else if(k==n)n=m;
	}
	cout<<n<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N =100010;
bool bol;
int n,a,b;
vector<int>v,vv;
map<int,int>mp;
int main(){
	cin>>n;
	for(int i=0;i<3;i++){
		cin>>a>>b;
		if(a==n)n=b;
		else if(b==n)n=a;
	}
	cout<<n<<endl;
	return 0;
}


#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <vector>
#include <bitset>
#include <fstream>
#include <string>
#include <sstream>
#include <cstring>

using namespace std;

typedef long long Long;

int main() {

    std::ios_base::sync_with_stdio(0);
#ifdef IDE
    freopen("C:\\file.in", "r", stdin);
//    freopen("C:\\file.out", "w", stdout);
#endif

    bool arr[10] = {};
    int x, a, b;
    cin >> x;
    arr[x] = 1;
    for (int i = 3; i--;) {
        cin >> a >> b;
        swap(arr[a], arr[b]);
    }
    for (int i = 1; i < 4; ++i)
        if (arr[i])
            printf("%d\n", i);
    return true & false;
}


#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <vector>
#include <bitset>
#include <fstream>
#include <string>
#include <sstream>
#include <cstring>

using namespace std;

typedef long long Long;

int main() {

    std::ios_base::sync_with_stdio(0);
#ifdef IDE
    freopen("C:\\file.in", "r", stdin);
//    freopen("C:\\file.out", "w", stdout);
#endif

    vector<bool> arr(10);
    int x, a, b;
    cin >> x;
    arr[x] = 1;
    for (int i = 0; i < 3; ++i) {
        cin >> a >> b;
        swap(arr[a], arr[b]);
    }
    for (int i = 1; i < 4; ++i)
        if (arr[i])
            printf("%d\n", i);

    return true & false;
}


#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <vector>
#include <bitset>
#include <fstream>
#include <string>
#include <sstream>
#include <cstring>

using namespace std;

typedef long long Long;

int main() {

    std::ios_base::sync_with_stdio(0);
#ifdef IDE
    freopen("C:\\file.in", "r", stdin);
//    freopen("C:\\file.out", "w", stdout);
#endif

    vector<bool> arr(10);
    int x, a, b;
    cin >> x;
    arr[x] = 1;
    for (int i = 3; i--;) {
        cin >> a >> b;
        swap(arr[a], arr[b]);
    }
    for (int i = 1; i < 4; ++i)
        if (arr[i])
            printf("%d\n", i);
    return true & false;
}


#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <vector>
#include <bitset>
#include <fstream>
#include <string>
#include <sstream>
#include <cstring>

using namespace std;

typedef long long Long;

int main() {

    std::ios_base::sync_with_stdio(0);
#ifdef IDE
    freopen("C:\\file.in", "r", stdin);
//    freopen('C:\\file.out', 'w', stdout);
#endif

    bool arr[4] = {};
    int x, a, b;
    cin >> x;
    arr[x] = 1;
    for (int i = 3; i--;) {
        cin >> a >> b;
        swap(arr[a], arr[b]);
    }
    for (int i = 1; i < 4; ++i)
        if (arr[i])
            printf("%d\n", i);
    return true & false;
}


#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <vector>
#include <bitset>
#include <fstream>
#include <string>
#include <sstream>
#include <cstring>

using namespace std;

typedef long long Long;

int main() {

    std::ios_base::sync_with_stdio(0);
#ifdef IDE
#endif
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    vector<bool> arr(10);
    int x, a, b;
    cin >> x;
    arr[x] = 1;
    for (int i = 3; i--;) {
        cin >> a >> b;
        swap(arr[a], arr[b]);
    }
    for (int i = 1; i < 4; ++i)
        if (arr[i])
            printf("%d\n", i);
    return true & false;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x;
    cin>>x;
    int a[3]={0};
    a[x-1]=1;
    for(int i=0;i<3;i++)
    {
        int x,b;
        cin>>x>>b;
        swap(a[x-1],a[b-1]);
    }
    for(int i=0;i<3;i++)
    if(a[i]==1)
    cout<<i+1;
    //end
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ifstream cin("input.txt");
ofstream cout("output.txt");
    int x;
    cin>>x;
    int a[3]={0};
    a[x-1]=1;
    for(int i=0;i<3;i++)
    {
        int x,b;
        cin>>x>>b;
        swap(a[x-1],a[b-1]);
    }
    for(int i=0;i<3;i++)
    if(a[i]==1)
    cout<<i+1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x=1;
    int a[3]={0};
    a[x-1]=1;
    for(int i=0;i<3;i++)
    {
        int x,b;
        cin>>x>>b;
        swap(a[x-1],a[b-1]);
    }
    for(int i=0;i<3;i++)
    if(a[i]==1)
    cout<<i+1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x;
    cin>>x;
    int a[3]={0};
    a[x-1]=1;
    for(int i=0;i<3;i++)
    {
        int x,b;
        cin>>x>>b;
        swap(a[x-1],a[b-1]);
    }
    for(int i=0;i<3;i++)
    if(a[i]==1)
    cout<<i+1;
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define PI          3.141592653589793
//to_string(i) used for converting i to string
//x<<y mutiplies x*pow(2,y)
using namespace std;
ll expo(ll x,ll n,ll p)
{

    if(n==0)
        return 1%p;
    ll ans=1,base=x;
    while(n>0)
    {

        if(n%2==1)
        {
            ans=(ans*base)%p;
            n--;
        }
        else
        {
            base=(base*base)%p;
            n/=2;
        }
    }
    if(ans<0)
        ans=(ans+p)%p;

    return ans;
}

ll power(ll x,ll y)
{
    ll k=1;
    while(y--)
    {
        k=k*x;
    }
    return k;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 ll n,p,a,b,i,x,y;
 cin>>n;
 p=n;

 for(i=0;i<3;i++)
 {
    cin>>a>>b;
    x=a;
    y=b;
    if(x==p)
        p=y;
    else if(p==y)
    p=x;

 }
cout<<p;

}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define PI          3.141592653589793
//to_string(i) used for converting i to string
//x<<y mutiplies x*pow(2,y)
using namespace std;
ll expo(ll x,ll n,ll p)
{

    if(n==0)
        return 1%p;
    ll ans=1,base=x;
    while(n>0)
    {

        if(n%2==1)
        {
            ans=(ans*base)%p;
            n--;
        }
        else
        {
            base=(base*base)%p;
            n/=2;
        }
    }
    if(ans<0)
        ans=(ans+p)%p;

    return ans;
}

ll power(ll x,ll y)
{
    ll k=1;
    while(y--)
    {
        k=k*x;
    }
    return k;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 ll n,p,a,b,i;
 cin>>n;
 p=n;
 for(i=0;i<3;i++)
 {
    cin>>a>>b;
    if(a==p)
        p=b;
    else if(p==b)
    p=a;

 }
cout<<p;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

 int a[3]={0},n,i,j,k;
 cin>>n;
 a[n-1]=1;
 for(i=0;i<3;i++)
 {
     cin>>j>>k;
     swap(a[j-1],a[k-1]);

 }
 for(i=0;i<3;i++)
 {
     if(a[i]==1)
     {
         cout<<i+1;
         break;
     }
 }
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define PI          3.141592653589793
//to_string(i) used for converting i to string
//x<<y mutiplies x*pow(2,y)
using namespace std;
ll expo(ll x,ll n,ll p)
{

    if(n==0)
        return 1%p;
    ll ans=1,base=x;
    while(n>0)
    {

        if(n%2==1)
        {
            ans=(ans*base)%p;
            n--;
        }
        else
        {
            base=(base*base)%p;
            n/=2;
        }
    }
    if(ans<0)
        ans=(ans+p)%p;

    return ans;
}

ll power(ll x,ll y)
{
    ll k=1;
    while(y--)
    {
        k=k*x;
    }
    return k;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 ll a[3]={0},n,x,y,i;
 cin>>n;
 if((n-1)<3)
a[n-1]=1;
for(i=0;i<3;i++)
{
    cin>>x>>y;
    swap(a[x-1],a[y-1]);
}
  for(i=0;i<3;i++)
  {
      if(a[i]>0)
        break;
  }
  cout<<i+1;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

 int a[3]={0},n,i,j,k;
 cin>>n;
 if(n-1>=0&&n-1<3)
 a[n-1]++;
 for(i=0;i<3;i++)
 {
     cin>>j>>k;
     swap(a[j-1],a[k-1]);

 }
 for(i=0;i<3;i++)
 {
     if(a[i]==1)
     {
         cout<<i+1;
         break;
     }
 }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

 int a[3]={0},n,i,j,k;
 cin>>n;
 a[n-1]++;
 for(i=0;i<3;i++)
 {
     cin>>j>>k;
     swap(a[j-1],a[k-1]);

 }
 for(i=0;i<3;i++)
 {
     if(a[i]==1)
     {
         cout<<i+1;
         break;
     }
 }
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define PI          3.141592653589793
//to_string(i) used for converting i to string
//x<<y mutiplies x*pow(2,y)
using namespace std;
ll expo(ll x,ll n,ll p)
{

    if(n==0)
        return 1%p;
    ll ans=1,base=x;
    while(n>0)
    {

        if(n%2==1)
        {
            ans=(ans*base)%p;
            n--;
        }
        else
        {
            base=(base*base)%p;
            n/=2;
        }
    }
    if(ans<0)
        ans=(ans+p)%p;

    return ans;
}

ll power(ll x,ll y)
{
    ll k=1;
    while(y--)
    {
        k=k*x;
    }
    return k;
}

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 ll n,p,a,b,i,x,y;
 cin>>n;
 p=n;

 for(i=0;i<3;i++)
 {
    cin>>a>>b;
    x=a;
    y=b;
    if(x==p)
        p=y;
    else if(p==y)
    p=x;

 }
cout<<p;

}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define PI          3.141592653589793
//to_string(i) used for converting i to string
//x<<y mutiplies x*pow(2,y)
using namespace std;
ll expo(ll x,ll n,ll p)
{

    if(n==0)
        return 1%p;
    ll ans=1,base=x;
    while(n>0)
    {

        if(n%2==1)
        {
            ans=(ans*base)%p;
            n--;
        }
        else
        {
            base=(base*base)%p;
            n/=2;
        }
    }
    if(ans<0)
        ans=(ans+p)%p;

    return ans;
}

ll power(ll x,ll y)
{
    ll k=1;
    while(y--)
    {
        k=k*x;
    }
    return k;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 ll n,p,a,b,i;
 cin>>n;
 p=n;
 for(i=0;i<3;i++)
 {
    cin>>a>>b;
    if(a==p)
        p=b;
    else if(p==b)
    p=a;

 }
cout<<a;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

 int a[3]={0},n,i,j,k;
 cin>>n;
 for(i=0;i<3;i++)
 {
    if(i==n-1)
        a[i]=1;
    else
        a[i]=0;
 }
 for(i=0;i<3;i++)
 {
     cin>>j>>k;

     swap(a[j-1],a[k-1]);

 }
 for(i=0;i<3;i++)
 {
     if(a[i]==1)
     {
         cout<<i+1;
         break;
     }
 }
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

 int a[3],n,i,j,k;
 cin>>n;
 for(i=0;i<3;i++)
 {
    if(i==n-1)
        a[i]=1;
    else
        a[i]=0;
 }
 for(i=0;i<3;i++)
 {
     cin>>j>>k;

     swap(a[j-1],a[k-1]);

 }
 for(i=0;i<3;i++)
 {
     if(a[i]==1)
     {
         cout<<i+1;
         break;
     }
 }
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define PI          3.141592653589793
//to_string(i) used for converting i to string
//x<<y mutiplies x*pow(2,y)
using namespace std;
ll expo(ll x,ll n,ll p)
{

    if(n==0)
        return 1%p;
    ll ans=1,base=x;
    while(n>0)
    {

        if(n%2==1)
        {
            ans=(ans*base)%p;
            n--;
        }
        else
        {
            base=(base*base)%p;
            n/=2;
        }
    }
    if(ans<0)
        ans=(ans+p)%p;

    return ans;
}

ll power(ll x,ll y)
{
    ll k=1;
    while(y--)
    {
        k=k*x;
    }
    return k;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 ll a[3]={0},n,x,y,i;
 cin>>n;
 for(i=0;i<n;i++)
 {
     if((i+1)==n)
     {
         a[i]=1;
         break;
     }
 }
for(i=0;i<3;i++)
{
    cin>>x>>y;
    swap(a[x-1],a[y-1]);
}
  for(i=0;i<3;i++)
  {
      if(a[i]>0)
        break;
  }
  cout<<i+1;

}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define PI          3.141592653589793
//to_string(i) used for converting i to string
//x<<y mutiplies x*pow(2,y)
using namespace std;
ll expo(ll x,ll n,ll p)
{

    if(n==0)
        return 1%p;
    ll ans=1,base=x;
    while(n>0)
    {

        if(n%2==1)
        {
            ans=(ans*base)%p;
            n--;
        }
        else
        {
            base=(base*base)%p;
            n/=2;
        }
    }
    if(ans<0)
        ans=(ans+p)%p;

    return ans;
}

ll power(ll x,ll y)
{
    ll k=1;
    while(y--)
    {
        k=k*x;
    }
    return k;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 int n,p,a,b,i,x,y;
 cin>>n;
 p=n;

 for(i=0;i<3;i++)
 {
    cin>>a>>b;
    x=a;
    y=b;
    if(x==p)
        p=y;
    else if(p==y)
    p=x;

 }
cout<<p;

}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define PI          3.141592653589793
//to_string(i) used for converting i to string
//x<<y mutiplies x*pow(2,y)
using namespace std;
ll expo(ll x,ll n,ll p)
{

    if(n==0)
        return 1%p;
    ll ans=1,base=x;
    while(n>0)
    {

        if(n%2==1)
        {
            ans=(ans*base)%p;
            n--;
        }
        else
        {
            base=(base*base)%p;
            n/=2;
        }
    }
    if(ans<0)
        ans=(ans+p)%p;

    return ans;
}

ll power(ll x,ll y)
{
    ll k=1;
    while(y--)
    {
        k=k*x;
    }
    return k;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 vi v,x,y;
 ll n,f,k,i;
 cin>>n;
 for(i=0;i<3;i++)
 {
     cin>>f>>k;
     x.pb(f);
     y.pb(k);
     v.pb(0);

 }
 v[n-1]=1;
 for(i=0;i<3;i++)
 {
     swap(v[x[i]-1],v[y[i]-1]);
 }
 for(i=0;i<3;i++)
 {
     if(v[i]==1)
        break;
 }

 cout<<i+1;

}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define PI          3.141592653589793
//to_string(i) used for converting i to string
//x<<y mutiplies x*pow(2,y)
using namespace std;
ll expo(ll x,ll n,ll p)
{

    if(n==0)
        return 1%p;
    ll ans=1,base=x;
    while(n>0)
    {

        if(n%2==1)
        {
            ans=(ans*base)%p;
            n--;
        }
        else
        {
            base=(base*base)%p;
            n/=2;
        }
    }
    if(ans<0)
        ans=(ans+p)%p;

    return ans;
}

ll power(ll x,ll y)
{
    ll k=1;
    while(y--)
    {
        k=k*x;
    }
    return k;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 ll a[3]={0},n,x,y,i;
 cin>>n;
a[n-1]=1;
for(i=0;i<3;i++)
{
    cin>>x>>y;
    swap(a[x-1],a[y-1]);
}
  for(i=0;i<3;i++)
  {
      if(a[i]>0)
        break;
  }
  cout<<i+1;

}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define PI          3.141592653589793
//to_string(i) used for converting i to string
//x<<y mutiplies x*pow(2,y)
using namespace std;
ll expo(ll x,ll n,ll p)
{

    if(n==0)
        return 1%p;
    ll ans=1,base=x;
    while(n>0)
    {

        if(n%2==1)
        {
            ans=(ans*base)%p;
            n--;
        }
        else
        {
            base=(base*base)%p;
            n/=2;
        }
    }
    if(ans<0)
        ans=(ans+p)%p;

    return ans;
}

ll power(ll x,ll y)
{
    ll k=1;
    while(y--)
    {
        k=k*x;
    }
    return k;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 ll a[3]={0},n,x,y,i;
 cin>>n;
a[n-1]++;
for(i=0;i<3;i++)
{
    cin>>x>>y;
    swap(a[x-1],a[y-1]);
}
  for(i=0;i<3;i++)
  {
      if(a[i]>0)
        break;
  }
  cout<<i+1;

}


#include<cstdio>
#include <iostream>
#include<cstring>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
#define N 10
using namespace std;
struct node{
int num;
int a;
}p[10][10];

int main(){freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int x,a[10][10],i,j,b[4];
scanf("%d",&x);
for (i=1;i<=3;i++){
for (j=1;j<=2;j++){
scanf("%d",&p[i][j].num);
if (p[i][j].num==x){p[i][j].a=1;}
else {p[i][j].a=0;}
}
}
for (i=1;i<=3;i++){
int m=1;
m=p[i][1].a;
p[i][1].a=p[i][2].a;
p[i][2].a=m;
}
for (i=1;i<=3;i++){
for (j=1;j<=2;j++){
if (p[i][j].a==1){printf("%d",p[i][j].num);return 0;}
}
}

}


#include<cstdio>
#include <iostream>
#include<cstring>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
#define N 10
using namespace std;
int main(){freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int x,a[10][10],i,j,b[4];
scanf("%d",&x);
for (i=1;i<=3;i++){
for (j=1;j<=2;j++){
scanf("%d",&a[i][j]);
}
}int m=x,p=0;
for (i=1;i<=3;i++){
if (a[i][1]==m){
    int temp;
    temp=a[i][1];
    a[i][1]=a[i][2];
    a[i][2]=temp;
    m=a[i][2];
    b[p]=a[i][1];p++;
}
else if (a[i][2]==m){
    int temp;
    temp=a[i][1];
    a[i][1]=a[i][2];
    a[i][2]=temp;
    m=a[i][1];
    b[p]=a[i][2];p++;

}
else {continue;}
}
printf("%d",b[2]);return 0;
}


#include<cstdio>
#include <iostream>
#include<cstring>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
#define N 10
using namespace std;
int main(){freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int x,a[10][10],i,j,b[4];
scanf("%d",&x);
for (i=1;i<=3;i++){
for (j=1;j<=2;j++){
scanf("%d",&a[i][j]);
}
}int m=x,p=0;
for (i=1;i<=3;i++){
if (a[i][1]==m){
    int temp;
    temp=a[i][1];
    a[i][1]=a[i][2];
    a[i][2]=temp;
    m=a[i][2];
    b[p]=a[i][1];p++;
}
else if (a[i][2]==m){
    int temp;
    temp=a[i][1];
    a[i][1]=a[i][2];
    a[i][2]=temp;
    m=a[i][1];
    b[p]=a[i][2];p++;

}
else {continue;}
}
printf("%d",b[p-1]);return 0;
}


#include<cstdio>
#include <iostream>
#include<cstring>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
#define N 10
using namespace std;
int main(){freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int x,a[10][10],i,j,b[4];
scanf("%d",&x);
for (i=1;i<=3;i++){
for (j=1;j<=2;j++){
scanf("%d",&a[i][j]);
}
}int m=x,p=0;
for (i=1;i<=3;i++){
if (a[i][1]==m){
    m=a[i][2];
    b[p]=a[i][2];p++;
}
else if (a[i][2]==m){
    m=a[i][1];
    b[p]=a[i][1];p++;

}
else {continue;}
}
printf("%d",b[p-1]);return 0;
}


#include<cstdio>
#include <iostream>
#include<cstring>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
#define N 10
using namespace std;
struct node{
int num;
int a;
}p[10][10];
int main(){
int x,a[10][10],i,j,b[4];
scanf("%d",&x);
for (i=1;i<=3;i++){
for (j=1;j<=2;j++){
scanf("%d",&p[i][j].num);
if (p[i][j].num==x){p[i][j].a=1;}
else {p[i][j].a=0;}
}
}
for (i=1;i<=3;i++){
int m=1;
m=p[i][1].a;
p[i][1].a=p[i][2].a;
p[i][2].a=m;
}
for (i=1;i<=3;i++){
for (j=1;j<=2;j++){
if (p[i][j].a==1){printf("%d",p[i][j].num);return 0;}
}
}

}


#include <iostream>
using namespace std;

int main() {
    int n,a,b;
   cin>>n;

    int p,q;
        for(int i=0;i<3;i++)
        {
           cin>>p>>q;
           a=p;
           b=q;
            if(a==n)
            n=b;
            else if(b==n)
            n=a;
        }
        cout<<n;
    return 0;
}


// Thoda Bheja lagaya hota to yaha dekhne ki zroort na padti

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

#define print(a)        for (auto x : a) cout << x << " "; cout << endl
#define print_upto(a,n)        for(int i=0;i<n;i++)    cout<<a[i]<<" "; cout<<endl
#define take(x,n)           for(int i=0;i<n;i++)  cin>>x[i];
#define rep(i,n)        for(int i=0;i<n;i++)
#define repange(i,a,b)      for(int i=a;i<=b;i++)
#define reprange(i,a,b)     for(int i=a;i>=b;i--)

void fast();

#define ll long long
#define ull unsigned long long
#define ldob long double
#define dob double
#define F first
#define S second
#define pii pair<int, int>
#define vec(a) vector<a>
#define umap(a, b) unordered_map<a, b>
#define endl "\n"
#define MOD 1000000007
#define inf 1e18
#define pb push_back
#define mp make_pair
#define int long long int
#define N 200005
int ar[N];
int n,a,b,c,d,m,k,q,mod;
vec(int) v;

int32_t main()
{
    fast();

    int t=1;    //change t if required
    //cin>>t;
    while(t--)
    {
        cin>>n;

    int p,q;
        for(int i=0;i<3;i++)
        {
           cin>>p>>q;
           a=p;
           b=q;
            if(a==n)
            n=b;
            else if(b==n)
            n=a;
        }
        cout<<n;
    return 0;

    }

    return 0;
}

void fast()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    ifstream ifile;
    ofstream ofile;
    ifile.open("input.txt");
    ofile.open("output.txt");
    int ballCupNumber;
    ifile>>ballCupNumber;
    for(int i=0;i<3;i++)
    {
        int cup1,cup2;
        ifile>>cup1;
        ifile>>cup2;
        if(cup1==ballCupNumber)
        {
            ballCupNumber=cup2;
        }
        else if(cup2==ballCupNumber)
        {
            ballCupNumber=cup1;
        }
    }
    ofile<<ballCupNumber<<"\n";
    ofile.close();
    ifile.close();
}


#include <iostream>
using namespace std;

int main() {
    int n,a,b;
   cin>>n;

		for(int i=0;i<3;i++)
		{
		   cin>>a;
		   cin>>b;
		    if(a==n)
		    n=b;
		    else if(b==n)
		    n=a;
		}
		cout<<n;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int t,x;
    cin>>t;
    long long int a,b,c,d,e,f;
    cin>>a>>b>>c>>d>>e>>f;
    if(t==a)
        x=b;
    else if(t==b)
        x=a;
    else
        x=t;
    if(x==c)
        x=d;
    else if(x==d)
        x=c;
    if(x==e)
        x=f;
    else if(x==f)
        x=e;
    cout<<x<<endl;
}


/*Bismillahir Rahmanir Rahim
->>Code by aimon
->>CUET,CSE
*/
#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
#define pb push_back
#define endl "\n"
#define MX INT_MAX
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define vi vector<int>
int main()
{
    #ifndef online_judge
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    long long int t,x;
    cin>>t;
    x=t;
    for(int i=0; i<3; i++)
    {
        long long int a,b;
        cin>>a>>b;
        if(x==a)
            x=b;
        else if(x==b)
            x=a;
    }
     cout<<x<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    int a,b,c,d,e,f;
    cin>>a>>b;
    if(t==a)
        t=b;
    else if(t==b)
        t=a;
    cin>>c>>d;
    if(t==c)
        t=d;
    else if(t==d)
        t=c;
    cin>>e>>f;
    if(t==e)
        t=f;
    else if(t==f)
        t=e;
    cout<<t<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int t;
    cin>>t;
    long long int a,b,c,d,e,f;
    cin>>a>>b>>c>>d>>e>>f;
    if(t==a)
        t=b;
    else if(t==b)
        t=a;
    if(t==c)
        t=d;
    else if(t==d)
        t=c;
    if(t==e)
        t=f;
    else if(t==f)
        t=e;
    cout<<t<<endl;
}


#include <bits/stdc++.h>
#define faster     ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define read       freopen("input.txt", "r", stdin);
#define write      freopen("output.txt", "w", stdout);
#define end        '\n'
#define ll         long long
using namespace std;
int main()
{
    long long int t,x;
    cin>>t;
    long long int a,b,c,d,e,f;
    cin>>a>>b>>c>>d>>e>>f;
    if(t==a)
        x=b;
    else if(t==b)
        x=a;
    else
        x=t;
    if(x==c)
        x=d;
    else if(x==d)
        x=c;
    if(x==e)
        x=f;
    else if(x==f)
        x=e;
    cout<<x<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
#define pb push_back
#define endl "\n"
#define MX INT_MAX
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define vi vector<int>
int main()
{
    #ifndef online_judge
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    long long int t,x;
    cin>>t;
    long long int a,b,c,d,e,f;
    cin>>a>>b>>c>>d>>e>>f;
    if(t==a)
        x=b;
    else if(t==b)
        x=a;
    else
        x=t;
    if(x==c)
        x=d;
    else if(x==d)
        x=c;
    if(x==e)
        x=f;
    else if(x==f)
        x=e;
    cout<<x<<endl;
}


#include<bits/stdc++.h>
#define pb push_back
#define FOR(i, a, b) for(short i=a; i<b; ++i)
#define FORR(i, a, b) for(int i=a; i>b; --i)
#define ll long long
using namespace std;

int main() {

// 	#ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// 	#endif

	short n, cups[3]={0}, a, b;
	cin>>n;
	cups[n-1]=-1;
	for(short i=0; i<3; ++i) {
		cin>>a>>b;
		swap(cups[a-1], cups[b-1]);
	}
	for(short i=0; i<3; ++i) if(cups[i]==-1) cout<<i+1;

	return 0;

}


#include<bits/stdc++.h>
#define pb push_back
#define FOR(i, a, b) for(short i=a; i<b; ++i)
#define FORR(i, a, b) for(int i=a; i>b; --i)
#define ll long long
using namespace std;

int main() {

// 	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
// 	#endif

	short n, a, b;
	cin>>n;
	for(short i=0; i<3; ++i) {
		cin>>a>>b;
		if(n==a) n=b;
		else if(n==b) n=a;
	}

	cout<<n;
	return 0;

}


#include<bits/stdc++.h>
#define pb push_back
#define FOR(i, a, b) for(short i=a; i<b; ++i)
#define FORR(i, a, b) for(int i=a; i>b; --i)
#define ll long long
using namespace std;

int main() {

	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	short n, a, b;
	cin>>n;
	for(short i=0; i<3; ++i) {
		cin>>a>>b;
		if(n==a) n=b;
		else if(n==b) n=a;
	}

	cout<<n;
	return 0;

}


#include<bits/stdc++.h>
#define pb push_back
#define FOR(i, a, b) for(short i=a; i<b; ++i)
#define FORR(i, a, b) for(int i=a; i>b; --i)
#define ll long long
using namespace std;

int main() {

// 	#ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// 	#endif

	short n, cups[4]={0}, a, b;
	cin>>n;
	cups[n]=-1;
	for(short i=1; i<4; ++i) {
		cin>>a>>b;
		swap(cups[a], cups[b]);
	}
	for(short i=1; i<4; ++i) if(cups[i]==-1) cout<<i;

	return 0;

}


#include<bits/stdc++.h>
#define pb push_back
#define FOR(i, a, b) for(short i=a; i<b; ++i)
#define FORR(i, a, b) for(int i=a; i>b; --i)
#define ll long long
using namespace std;

int main() {

// 	#ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// 	#endif

	short n, a, b;
	cin>>n;
	for(short i=0; i<3; ++i) {
		cin>>a>>b;
		if(n==a) n=b;
		else if(n==b) n=a;
	}

	cout<<n;
	return 0;

}


#include<bits/stdc++.h>
#define pb push_back
#define FOR(i, a, b) for(short i=a; i<b; ++i)
#define FORR(i, a, b) for(int i=a; i>b; --i)
#define ll long long
using namespace std;

int main() {

	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	int n, cups[4]={0}, a, b;
	cin>>n;
	cups[n]=-1;
	for(short i=1; i<4; ++i) {
		cin>>a>>b;
		swap(cups[a], cups[b]);
	}
	for(short i=1; i<4; ++i) if(cups[i]==-1) cout<<i;

	return 0;

}


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

#define ll long long

const int sz = 1e9 + 7;

void autocheck() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	autocheck();
	int t = 1;
	// cin >> t;
	while (t--) {
		int ans = 0; cin >> ans;
		int x = 0, y = 0;

		for (int i = 0; i < 3; i++) {
			cin >> x >> y;
			if (x == ans) {
				ans = y;
			}
			else if (y == ans) {
				ans = x;
			}
		}
		cout << ans << endl;
	}
}


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

#define ll long long

const int sz = 1e9 + 7;

void autocheck() {
#ifndef ONLINE_JUDGE
	freopen("inputf.in", "r", stdin);
	freopen("outputf.in", "w", stdout);
#endif
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	autocheck();
	int t = 1;
	// cin >> t;
	while (t--) {
		int ans; cin >> ans;
		int x = 0, y = 0;

		for (int i = 0; i < 3; i++) {
			cin >> x >> y;
			if (x == ans) {
				ans = y;
			}
			else if (y == ans) {
				ans = x;
			}
		}
		cout << ans << endl;
	}
}


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

#define ll long long

const int sz = 1e9 + 7;

void autocheck() {
#ifndef ONLINE_JUDGE
	freopen("inputf.in", "r", stdin);
	freopen("outputf.in", "w", stdout);
#endif
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	// autocheck();
	int t = 1;
	// cin >> t;
	while (t--) {
		int ans = 0; cin >> ans;
		int x = 0, y = 0;

		for (int i = 0; i < 3; i++) {
			cin >> x >> y;
			if (x == ans) {
				ans = y;
			}
			else if (y == ans) {
				ans = x;
			}
		}
		cout << ans << endl;
	}
}


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

#define ll long long

const int sz = 1e9 + 7;

void autocheck() {
#ifndef ONLINE_JUDGE
	freopen("inputf.in", "r", stdin);
	freopen("outputf.in", "w", stdout);
#endif
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	autocheck();
	int t = 1;
	// cin >> t;
	while (t--) {
		int ans; cin >> ans;
		int x, y;

		for (int i = 0; i < 3; i++) {
			cin >> x >> y;
			if (x == ans) {
				ans = y;
			}
			else if (y == ans) {
				ans = x;
			}
		}
		cout << ans << endl;
	}
}


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

#define ll long long

const int sz = 1e9 + 7;

void autocheck() {
#ifndef ONLINE_JUDGE
	freopen("inputf.in", "r", stdin);
	freopen("outputf.in", "w", stdout);
#endif
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	autocheck();
	int t = 1;
	// cin >> t;
	while (t--) {
		int ans = 0; cin >> ans;
		int x, y;

		for (int i = 0; i < 3; i++) {
			cin >> x >> y;
			if (x == ans) {
				ans = y;
			}
			else if (y == ans) {
				ans = x;
			}
		}
		cout << ans << endl;
	}
}


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

#define ll long long

const int sz = 1e9 + 7;

void autocheck() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	// autocheck();
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	int t = 1;
	// cin >> t;
	while (t--) {
		int ans = 0; cin >> ans;
		int x = 0, y = 0;

		for (int i = 0; i < 3; i++) {
			cin >> x >> y;
			if (x == ans) {
				ans = y;
			}
			else if (y == ans) {
				ans = x;
			}
		}
		cout << ans << endl;
	}
}


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

#define ll long long

const int sz = 1e9 + 7;

void autocheck() {
#ifndef ONLINE_JUDGE
	freopen("inputf.in", "r", stdin);
	freopen("outputf.in", "w", stdout);
#endif
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	autocheck();
	int t = 1;
	// cin >> t;
	while (t--) {
		int ans = 0; cin >> ans;
		int x = 0, y = 0;

		for (int i = 0; i < 3; i++) {
			cin >> x >> y;
			if (x == ans) {
				ans = y;
			}
			else if (y == ans) {
				ans = x;
			}
		}
		cout << ans << endl;
	}
}


#include<bits/stdc++.h>
#define com(i,n)    for(int i=0;i<n;i++)
#define dom(i,n)    for(int i=1;i<=n;i++)
#define mom(i,n)    for(int i=n;i>=0;i--)
#define sortI(a,n)  sort(a,a+n)
#define sortD(a,n)  sort(a,a+n,greater<int>())
using namespace std;
int main()
{
    int x,y;
    cin>>x;
    y=x;
    com(i,3)
    {
        int a,b;
        cin>>a>>b;
        if(y==a)
        {
            y=b;
        }
        else if(y==b)
        {
            y=a;
        }
        else
            continue;
    }
    cout << y << endl;
}


#include<bits/stdc++.h>
#define com(i,n)    for(int i=0;i<n;i++)
#define dom(i,n)    for(int i=1;i<=n;i++)
#define mom(i,n)    for(int i=n;i>=0;i--)
#define sortI(a,n)  sort(a,a+n)
#define sortD(a,n)  sort(a,a+n,greater<int>())
using namespace std;
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    int x;
    cin>>x;
    int t=3;
    while(t--)
    {
        int a,b;
        cin>>a>>b;
        if(x==a)
        {
            x=b;
        }
        else if(x==b)
        {
            x=a;
        }

    }
    cout<<x<<endl;
}


#include<bits/stdc++.h>
#define com(i,n)    for(int i=0;i<n;i++)
#define dom(i,n)    for(int i=1;i<=n;i++)
#define mom(i,n)    for(int i=n;i>=0;i--)
#define sortI(a,n)  sort(a,a+n)
#define sortD(a,n)  sort(a,a+n,greater<int>())
using namespace std;
int main()
{
    int x,y[4],temp;
    cin>>x;
    y[1]=1;
    y[2]=2;
    y[3]=3;
    dom(i,3)
    {
        int a,b;
        cin>>a>>b;
        swap(y[a],y[b]);
    }
    dom(i,3)
    {
         //cout << y[i] << " ";
        if(y[i]==x)
            cout << i << endl;
        //return 0;
    }
}


#include<bits/stdc++.h>
#define com(i,n)    for(int i=0;i<n;i++)
#define dom(i,n)    for(int i=1;i<=n;i++)
#define mom(i,n)    for(int i=n;i>=0;i--)
#define sortI(a,n)  sort(a,a+n)
#define sortD(a,n)  sort(a,a+n,greater<int>())
using namespace std;
int main()
{
     int x;
     cin>>x;
     com(i,3)
     {
          int a,b;
          cin>>a>>b;
          if(a==x && a!=b)
               x=b;
          else if(b==x && a!=b)
               x=a;
     }
     cout << x << endl;
}


#include<bits/stdc++.h>
#define com(i,n)    for(int i=0;i<n;i++)
#define dom(i,n)    for(int i=1;i<=n;i++)
#define mom(i,n)    for(int i=n;i>=0;i--)
#define sortI(a,n)  sort(a,a+n)
#define sortD(a,n)  sort(a,a+n,greater<int>())
using namespace std;
int main()
{
    int x;
    cin>>x;
    com(i,3)
    {
        int a,b;
        cin>>a>>b;
        if(x==a)
        {
            x=b;
        }
        else if(x==b)
        {
            x=a;
        }
        else
            continue;
    }
    cout << x << endl;
}


#include<bits/stdc++.h>
#define com(i,n)    for(int i=0;i<n;i++)
#define dom(i,n)    for(int i=1;i<=n;i++)
#define mom(i,n)    for(int i=n;i>=0;i--)
#define sortI(a,n)  sort(a,a+n)
#define sortD(a,n)  sort(a,a+n,greater<int>())
using namespace std;
int main()
{
     int x,a[3],b[3];
     cin>>x;
     com(i,3)
     {
          cin>>a[i]>>b[i];
          if(x==a[i])
               x=b[i];
          else if(x==b[i])
               x=a[i];
     }
     cout << x << endl;
}


#include<bits/stdc++.h>
#define com(i,n)    for(int i=0;i<n;i++)
#define dom(i,n)    for(int i=1;i<=n;i++)
#define mom(i,n)    for(int i=n;i>=0;i--)
#define sortI(a,n)  sort(a,a+n)
#define sortD(a,n)  sort(a,a+n,greater<int>())
using namespace std;
int main()
{
    int x;
    cin>>x;
    int t=3;
    while(t--)
    {
        int a,b;
        cin>>a>>b;
        if(x==a)
        {
            x=b;
        }
        else if(x==b)
        {
            x=a;
        }

    }
    cout<<x<<endl;
}


#include<bits/stdc++.h>
#define com(i,n)    for(int i=0;i<n;i++)
#define dom(i,n)    for(int i=1;i<=n;i++)
#define mom(i,n)    for(int i=n;i>=0;i--)
#define sortI(a,n)  sort(a,a+n)
#define sortD(a,n)  sort(a,a+n,greater<int>())
using namespace std;
int main()
{
     int x,a,b;
     cin>>x;
     com(i,3)
     {
          cin>>a>>b;
          if(x==a)
               x=b;
          else if(x==b)
               x=a;
     }
     cout << x << endl;
}


#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include<set>
#include<iostream>
using namespace std;
inline int read()
{
	int s = 0, w = 1;
	char ch = getchar();
	while (ch <= '0' || ch>'9')
	{
		if (ch == '-')
			w = -1;
		ch = getchar();
	}
	while (ch >= '0'&&ch <= '9')
		s = s * 10 + ch - '0', ch = getchar();
	return s * w;
}
const int inf = 0x3f3f3f3f;
typedef long long LL;
const int M = 200005;
int k, n,sum[M],a[M];
struct num {
	int x, y;
}t[2*M];
int main()
{
	k = read();
	int flag = 0,sx,sy,sum1;
	for (int i = 1; i <= k; i++)
	{
		n = read();
		for (int j = 1; j <= n; j++)
		{
			a[j] = read();
			sum[i] += a[j];
		}
		for (int j = 1; j <= n&&flag==0; j++)
		{
			if (t[M+sum[i] - a[j]].x != 0&& t[M+sum[i] - a[j]].x!=i)
			{
				flag = 1;
				sx = i;
				sy = j;
				sum1=M+sum[i] - a[j];
				break;
			}
			t[M+sum[i] - a[j]].x = i;
			t[M+sum[i] - a[j]].y = j;
		}
	}
	if (flag)
	{
		printf("YES\n");
		printf("%d %d\n%d %d\n", t[sum1].x, t[sum1].y, sx, sy);
	}
	else
		printf("NO\n");
}


#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include<set>
#include<iostream>
using namespace std;
inline int read()
{
	int s = 0, w = 1;
	char ch = getchar();
	while (ch <= '0' || ch>'9')
	{
		if (ch == '-')
			w = -1;
		ch = getchar();
	}
	while (ch >= '0'&&ch <= '9')
		s = s * 10 + ch - '0', ch = getchar();
	return s * w;
}
const int inf = 0x3f3f3f3f;
typedef long long LL;
const int M = 200005;
LL k, n,sum[M],a[M];
struct num {
	int x, y;
}t[2*M];
int main()
{
	k = read();
	int flag = 0,sx,sy,sum1;
	for (int i = 1; i <= k; i++)
	{
		n = read();
		for (int j = 1; j <= n; j++)
		{
			a[j] = read();
			sum[i] += a[j];
		}
		for (int j = 1; j <= n&&flag==0; j++)
		{
			if (t[M+sum[i] - a[j]].x != 0&& t[M+sum[i] - a[j]].x!=i)
			{
				flag = 1;
				sx = i;
				sy = j;
				sum1=M+sum[i] - a[j];
				break;
			}
			t[M+sum[i] - a[j]].x = i;
			t[M+sum[i] - a[j]].y = j;
		}
	}
	if (flag)
	{
		printf("YES\n");
		printf("%d %d\n%d %d\n", t[sum1].x, t[sum1].y, sx, sy);
	}
	else
		printf("NO\n");
}


#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include<set>
#include<iostream>
using namespace std;
inline int read()
{
	int s = 0, w = 1;
	char ch = getchar();
	while (ch <= '0' || ch>'9')
	{
		if (ch == '-')
			w = -1;
		ch = getchar();
	}
	while (ch >= '0'&&ch <= '9')
		s = s * 10 + ch - '0', ch = getchar();
	return s * w;
}
const int inf = 0x3f3f3f3f;
typedef long long LL;
const int M = 200005;
int k, n,sum[M],a[M];
struct num {
	int x, y;
}t[M];
int main()
{
	k = read();
	int flag = 0,sx,sy,sum1;
	for (int i = 1; i <= k&&flag==0; i++)
	{
		n = read();
		for (int j = 1; j <= n; j++)
		{
			a[j] = read();
			sum[i] += a[j];
		}
		for (int j = 1; j <= n&&flag==0; j++)
		{
			if (t[sum[i] - a[j]].x != 0&& t[sum[i] - a[j]].x!=i)
			{
				flag = 1;
				sx = i;
				sy = j;
				sum1=sum[i] - a[j];
				break;
			}
			t[sum[i] - a[j]].x = i;
			t[sum[i] - a[j]].y = j;
		}
	}
	if (flag)
	{
		printf("YES\n");
		printf("%d %d\n%d %d\n", t[sum1].x, t[sum1].y, sx, sy);
	}
	else
		printf("NO\n");
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define PI acos(-1.0)
#define eps 1e-12
#define fi first
#define se second
#define MEM(a,b) memset((a),(b),sizeof(a))
#define mod(x) ((x)%MOD)
#define pii pair<int,int>
#define wz cout<<"-----"<<endl;
const int INF_INT = 2147483647;
const ll INF_LL = 9223372036854775807LL;
const ull INF_ULL = 18446744073709551615Ull;
const ll P = 92540646808111039LL;

const ll maxn = 2e5 + 10, MOD = 1e9 + 7;
const int Move[4][2] = {-1,0,1,0,0,1,0,-1};
const int Move_[8][2] = {-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void init() {

}

pair<ll,ll> s[maxn];
vector<ll> t;
int main(){
//    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
  //  freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
//    ios_base::sync_with_stdio(0);
    init();
    ll n; cin>>n;
    for (ll i = 1; i <= n; i++) {
        t.clear();
        ll l; cin>>l;
        ll sum = 0;
        for (ll j = 0; j < l; j++) {
            ll x; cin>>x;
            sum += x;
            t.push_back(x);
        }
        for (ll j = 0; j < l; j++) {
            ll sb = sum - t[j];
            if (s[sb].fi == i) continue;
            if (s[sb].fi == 0) {
                s[sb] = (pii) {i, j + 1};
            } else {
                puts("YES");
                cout<<s[sb].fi<<' '<<s[sb].se<<endl;
                cout<<i<<' '<<j + 1<<endl;
                return 0;
            }
        }
    }
    puts("NO");

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define PI acos(-1.0)
#define eps 1e-12
#define fi first
#define se second
#define MEM(a,b) memset((a),(b),sizeof(a))
#define mod(x) ((x)%MOD)
#define pii pair<int,int>
#define wz cout<<"-----"<<endl;
const int INF_INT = 2147483647;
const ll INF_LL = 9223372036854775807LL;
const ull INF_ULL = 18446744073709551615Ull;
const ll P = 92540646808111039LL;

const ll maxn = 2e5 + 10, MOD = 1e9 + 7;
const int Move[4][2] = {-1,0,1,0,0,1,0,-1};
const int Move_[8][2] = {-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void init() {

}

vector<vector<int> > a;

pair<int,int> s[maxn];

int main(){
//    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
  //  freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
//    ios_base::sync_with_stdio(0);
    init();
    int n; cin>>n;
    for (int i = 1; i <= n; i++) {
        vector<int> t;
        int l; cin>>l;
        ll sum = 0;
        for (int j = 0; j < l; j++) {
            int x; cin>>x;
            sum += x;
            t.push_back(x);
        }
        for (int j = 0; j < l; j++) {
            ll sb = sum - t[j];
            if (s[sb].fi == i || s[sb].fi == 0) {
                s[sb] = (pii) {i, j + 1};
            } else {
                puts("YES");
                cout<<s[sb].fi<<' '<<s[sb].se<<endl;
                cout<<i<<' '<<j + 1<<endl;
                return 0;
            }
        }
    }
    puts("NO");

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define fi first
#define se second

const ll maxn = 1e6 + 10;

void init() {

}

//pair<ll,ll> s[maxn];
//ll s[maxn];
//ll ss[maxn];
map<ll, int> s,ss;
ll t[maxn];

int main(){
    bool flg = false;
    ll w1,w2,w3,w4;
    ll n; cin>>n;
    for (ll i = 1; i <= n; i++) {
        ll l; cin>>l;
        ll sum = 0;
        for (ll j = 0; j < l; j++) {
            cin>>t[j];
            sum += t[j];
        }
        if (flg) continue;
        for (ll j = 0; j < l; j++) {
            ll sb = sum - t[j];
            if (s[sb] == i) continue;
            if (s[sb] == 0) {
                s[sb] = i;
                ss[sb] =  j + 1;
            } else {
                flg = true;
                w1 = s[sb], w2 = ss[sb], w3 = i, w4 = j + 1;
            }
        }
    }
    if (flg) {
        puts("YES");
        cout<<w1<<' '<<w2<<endl;
        cout<<w3<<' '<<w4<<endl;
    } else   puts("NO");

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define fi first
#define se second

const ll maxn = 3e5 + 10;

void init() {

}

//pair<ll,ll> s[maxn];
ll s[maxn];
ll ss[maxn];
ll t[maxn];

int main(){

    ll n; cin>>n;
    for (ll i = 1; i <= n; i++) {
        ll l; cin>>l;
        ll sum = 0;
        for (ll j = 0; j < l; j++) {
            cin>>t[j];
            sum += t[j];
        }
        for (ll j = 0; j < l; j++) {
            ll sb = sum - t[j];
            if (s[sb] == i) continue;
            if (s[sb] == 0) {
                s[sb] = i;
                ss[sb] =  j + 1;
            } else {
                puts("YES");
                cout<<s[sb]<<' '<<ss[sb]<<endl;
                cout<<i<<' '<<j + 1<<endl;
                return 0;
            }
        }
    }
    puts("NO");

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define PI acos(-1.0)
#define eps 1e-12
#define fi first
#define se second
#define MEM(a,b) memset((a),(b),sizeof(a))
#define mod(x) ((x)%MOD)
#define pii pair<int,int>
#define wz cout<<"-----"<<endl;
const int INF_INT = 2147483647;
const ll INF_LL = 9223372036854775807LL;
const ull INF_ULL = 18446744073709551615Ull;
const ll P = 92540646808111039LL;

const ll maxn = 3e5 + 10, MOD = 1e9 + 7;
const int Move[4][2] = {-1,0,1,0,0,1,0,-1};
const int Move_[8][2] = {-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void init() {

}

pair<ll,ll> s[maxn];
vector<ll> t;
int main(){
//    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
  //  freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
//    ios_base::sync_with_stdio(0);
    init();
    for (int i = 0; i < maxn; i++) {
        s[i] = (pii) {0,0};
    }
    ll n; cin>>n;
    for (ll i = 1; i <= n; i++) {
        t.clear();
        ll l; cin>>l;
        ll sum = 0;
        for (ll j = 0; j < l; j++) {
            ll x; cin>>x;
            sum += x;
            t.push_back(x);
        }
        for (ll j = 0; j < l; j++) {
            ll sb = sum - t[j];
            if (s[sb].fi == i) continue;
            if (s[sb].fi == 0) {
                s[sb] = (pii) {i, j + 1};
            } else {
                puts("YES");
                cout<<s[sb].fi<<' '<<s[sb].se<<endl;
                cout<<i<<' '<<j + 1<<endl;
                return 0;
            }
        }
    }
    puts("NO");

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define fi first
#define se second

const ll maxn = 3e5 + 10;

void init() {

}

//pair<ll,ll> s[maxn];
ll s[maxn];
ll ss[maxn];
ll t[maxn];

int main(){
    bool flg = false;
    ll w1,w2,w3,w4;
    ll n; cin>>n;
    for (ll i = 1; i <= n; i++) {
        ll l; cin>>l;
        ll sum = 0;
        for (ll j = 0; j < l; j++) {
            cin>>t[j];
            sum += t[j];
        }
        if (flg) continue;
        for (ll j = 0; j < l; j++) {
            ll sb = sum - t[j];
            if (s[sb] == i) continue;
            if (s[sb] == 0) {
                s[sb] = i;
                ss[sb] =  j + 1;
            } else {
                flg = true;
                w1 = s[sb], w2 = ss[sb], w3 = i, w4 = j + 1;
            }
        }
    }
    if (flg) {
        puts("YES");
        cout<<w1<<' '<<w2<<endl;
        cout<<w3<<' '<<w4<<endl;
    } else   puts("NO");

    return 0;
}