#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define fast ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int nax = 1e5 + 10;

int T;
vector<pair<int,int>> m;
set<pair<int,int>> check;
vector<int> g[nax];
int val[nax];

void add(int u,int t){
	if(check.find({u,t}) == check.end()){
		m.push_back({u,t});
		check.insert({u,t});
	}
}

int fr[nax];

int best(int u,int t){
	for(int i=fr[u];i<=T;i++){
		if(check.find({u,i}) == check.end()){
			fr[u] = i;
			return i;
		}
	}
	return T;
}

int dfs(int u,int p,int t,int b=0){
	add(u,t);
	val[u] = t;
	// cerr << u << '\n';
	vector<int> to;
	for(int v : g[u]){
		if(v == p)continue;
		to.push_back(v);
	}
	for(int i=0;i<(int)to.size()-1;i++){
		// if(u == 3)cerr << to.size() << '\n';
		int v = to[i];
		if(t == T){
			t = best(u,t);
			add(u,t);
		}
		t = dfs(v,u,t+1,0) + 1;
		add(u,t);
	}
	for(int i=T-1;i>=0;i--){
		if(check.find({u,i}) == check.end()){
			if(check.find({p,i+1}) == check.end() && i >= b-1){
				if(to.size()){
					// cerr << u << ' ' << to.back() << ' ' << i << '\n';
					if(t == T){
						t = best(u,t);
						add(u,t);
					}
					t = dfs(to.back(),u,t+1,i) + 1;
					add(u,t);
				}
				if(t != i && p){
					// assert(t > i);
					add(u,i);
				}
				return i;
			}
		}
	}
	if(t+1 <= T && check.find({p,t+1}) == check.end()){
		int w = t;
		if(to.size()){
			if(t == T){
				t = best(u,t);
				add(u,t);
			}
			t = dfs(to.back(),u,t+1,t+1) + 1;
			add(u,t);
			// if(t == T){
			// 	t = best(u,t);
			// 	add(u,t);
			// }
		}
		// if(t != w && p){
		// 	// assert(t > w);
		// 	cerr << u << ' ' << w << '\n';
		// 	add(u,w);
		// }
		if(t == T){
			t = best(u,t);
			add(u,t);
		}
		return t;
	}
	// if(p)
	// 	assert(0);
	return 0;
}

int main(){
	fast
	int n;
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=n;i++)T = max(T , (int)g[i].size());
	val[1]=1;
	dfs(1,0,0);
	// assert(check.size() == m.size());
	cout << m.size() << '\n';
	for(pair<int,int> i : m){
		cout << i.first << ' ' << i.second << '\n';
	}
}


#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define fast ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int nax = 1e5 + 10;

int T;
vector<pair<int,int>> m;
set<pair<int,int>> check;
vector<int> g[nax];
int val[nax];

void add(int u,int t){
	if(check.find({u,t}) == check.end()){
		m.push_back({u,t});
		check.insert({u,t});
	}
}

int dfs(int u,int p,int t,int in = 0){
	add(u,t);
	int e = g[u].size() - 1 + !p;
	int st = t;
	for(int v : g[u]){
		if(v == p)continue;
		if(t == T){
			t = st - e - 1;
			add(u,t);
		}
		e--;
		// cerr << u << ' ' << v << ' ' << t << '\n';
		int x = dfs(v,u,t+1,t);
		t = x + 1;
		add(u,t);
	}
	if(p && in != t){
		t = in;
		add(u,in);
	}
	return t;
}

int main(){
	fast
	int n;
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=n;i++)T = max(T , (int)g[i].size());
	val[1]=1;
	dfs(1,0,0);
	// assert(check.size() == m.size());
	cout << m.size() << '\n';
	for(pair<int,int> i : m){
		cout << i.first << ' ' << i.second << '\n';
	}
}


#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define fast ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int nax = 1e5 + 10;

int T;
vector<pair<int,int>> m;
set<pair<int,int>> check;
vector<int> g[nax];
int val[nax];

void add(int u,int t){
	m.push_back({u,t});
	check.insert({u,t});
}

int dfs(int u,int p,int t){
	add(u,t);
	val[u] = t;
	if(t == T){
		t = 0;
		add(u,t);
	}
	for(int v : g[u]){
		if(v == p)continue;
		int x = dfs(v,u,t+1) + 1;
		t = x;
		add(u,t);
	}
	for(int i=0;i<T;i++){
		if(check.find({u,i}) == check.end()){
			if(check.find({p,i+1}) == check.end()){
				// cerr << u << ' ' << i << '\n';
				add(u,i);
				return i;
			}
		}
	}
	if(t+1 <= T && check.find({p,t+1}) == check.end())return t;
	if(p)
		assert(0);
	return 0;
}

int main(){
	fast
	int n;
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=n;i++)T = max(T , (int)g[i].size());
	dfs(1,0,0);
	assert(check.size() == m.size());
	cout << m.size() << '\n';
	for(pair<int,int> i : m){
		cout << i.first << ' ' << i.second << '\n';
	}
}


#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define fast ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int nax = 1e5 + 10;

int T;
vector<pair<int,int>> m;
set<pair<int,int>> check;
vector<int> g[nax];
int val[nax];

void add(int u,int t){
	if(check.find({u,t}) == check.end()){
		m.push_back({u,t});
		check.insert({u,t});
	}
}

int dfs(int u,int p,int t,int in = 0){
	add(u,t);
	int e = g[u].size() - 1 + !p;
	int st = t;
	for(int v : g[u]){
		if(v == p)continue;
		if(t == T){
			t = st - e;
			add(u,t);
		}
		e--;
		// cerr << u << ' ' << v << ' ' << t << '\n';
		int x = dfs(v,u,t+1,t);
		t = x + 1;
		add(u,t);
	}
	if(p && in != t){
		t = in;
		add(u,in);
	}
	return t;
}

int main(){
	fast
	int n;
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=n;i++)T = max(T , (int)g[i].size());
	val[1]=1;
	dfs(1,0,0);
	// assert(check.size() == m.size());
	cout << m.size() << '\n';
	for(pair<int,int> i : m){
		cout << i.first << ' ' << i.second << '\n';
	}
}


#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define fast ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int nax = 1e5 + 10;

int T;
vector<pair<int,int>> m;
set<pair<int,int>> check;
vector<int> g[nax];
int val[nax];

void add(int u,int t){
	if(check.find({u,t}) == check.end()){
		m.push_back({u,t});
		check.insert({u,t});
	}
}

int fr[nax];

int best(int u,int t){
	for(int i=fr[u];i<=T;i++){
		if(check.find({u,i}) == check.end()){
			fr[u] = i;
			return i;
		}
	}
	return T;
}

int dfs(int u,int p,int t,int b=0){
	add(u,t);
	val[u] = t;
	// cerr << u << '\n';
	vector<int> to;
	for(int v : g[u]){
		if(v == p)continue;
		to.push_back(v);
	}
	for(int i=0;i<(int)to.size()-1;i++){
		// if(u == 3)cerr << to.size() << '\n';
		int v = to[i];
		if(t == T){
			t = best(u,t);
			add(u,t);
		}
		t = dfs(v,u,t+1,0) + 1;
		add(u,t);
	}
	for(int i=T-1;i>=0;i--){
		if(check.find({u,i}) == check.end()){
			if(check.find({p,i+1}) == check.end() && i >= b-1){
				if(to.size()){
					// cerr << u << ' ' << to.back() << ' ' << i << '\n';
					if(t == T){
						t = best(u,t);
						add(u,t);
					}
					t = dfs(to.back(),u,t+1,i) + 1;
					add(u,t);
				}
				if(t != i && p){
					// assert(t > i);
					add(u,i);
				}
				return i;
			}
		}
	}
	if(p && t+1 <= T && check.find({p,t+1}) == check.end()){
		int w = t;
		if(to.size()){
			if(t == T){
				t = best(u,t);
				add(u,t);
			}
			t = dfs(to.back(),u,t+1,t+1) + 1;
			add(u,t);
			// if(t == T){
			// 	t = best(u,t);
			// 	add(u,t);
			// }
		}
		// if(t != w && p){
		// 	// assert(t > w);
		// 	cerr << u << ' ' << w << '\n';
		// 	add(u,w);
		// }
		if(t == T){
			t = best(u,t);
			add(u,t);
		}
		return t;
	}
	// if(p)
	// 	assert(0);
	return 0;
}

int main(){
	fast
	int n;
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=n;i++)T = max(T , (int)g[i].size());
	val[1]=1;
	dfs(1,0,0);
	// assert(check.size() == m.size());
	cout << m.size() << '\n';
	for(pair<int,int> i : m){
		cout << i.first << ' ' << i.second << '\n';
	}
}


#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define fast ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int nax = 1e5 + 10;

int T;
vector<pair<int,int>> m;
set<pair<int,int>> check;
vector<int> g[nax];
int val[nax];

void add(int u,int t){
	if(check.find({u,t}) == check.end()){
		m.push_back({u,t});
		check.insert({u,t});
	}
}

int fr[nax];

int best(int u,int t){
	for(int i=fr[u];i<=T;i++){
		if(check.find({u,i}) == check.end()){
			fr[u] = i;
			return i;
		}
	}
	return T;
}

int dfs(int u,int p,int t,int b=0){
	add(u,t);
	val[u] = t;
	// cerr << u << '\n';
	vector<int> to;
	for(int v : g[u]){
		if(v == p)continue;
		to.push_back(v);
	}
	for(int i=0;i<(int)to.size()-1;i++){
		// if(u == 3)cerr << to.size() << '\n';
		int v = to[i];
		if(t == T){
			t = best(u,t);
			add(u,t);
		}
		t = dfs(v,u,t+1,0) + 1;
		add(u,t);
	}
	for(int i=T-1;i>=0;i--){
		if(check.find({u,i}) == check.end()){
			if(check.find({p,i+1}) == check.end() && i >= b-1){
				if(to.size()){
					// cerr << u << ' ' << to.back() << ' ' << i << '\n';
					if(t == T){
						t = best(u,t);
						add(u,t);
					}
					t = dfs(to.back(),u,t+1,i) + 1;
					add(u,t);
				}
				if(t != i && p){
					// assert(t > i);
					add(u,i);
				}
				return i;
			}
		}
	}
	if(p && t+1 <= T && check.find({p,t+1}) == check.end()){
		int w = t;
		if(to.size()){
			if(t == T){
				t = best(u,t);
				add(u,t);
			}
			t = dfs(to.back(),u,t+1,t+1) + 1;
			add(u,t);
			// if(t == T){
			// 	t = best(u,t);
			// 	add(u,t);
			// }
		}
		// if(t != w && p){
		// 	// assert(t > w);
		// 	cerr << u << ' ' << w << '\n';
		// 	add(u,w);
		// }
		return t;
	}
	// if(p)
	// 	assert(0);
	return 0;
}

int main(){
	fast
	int n;
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=n;i++)T = max(T , (int)g[i].size());
	val[1]=1;
	dfs(1,0,0);
	// assert(check.size() == m.size());
	cout << m.size() << '\n';
	for(pair<int,int> i : m){
		cout << i.first << ' ' << i.second << '\n';
	}
}


#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define fast ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int nax = 1e5 + 10;

int T;
vector<pair<int,int>> m;
set<pair<int,int>> check;
vector<int> g[nax];
int val[nax];

void add(int u,int t){
	if(check.find({u,t}) == check.end()){
		m.push_back({u,t});
		check.insert({u,t});
	}
}

int fr[nax];

int best(int u,int t){
	for(int i=fr[u];i<=T;i++){
		if(check.find({u,i}) == check.end()){
			fr[u] = i;
			return i;
		}
	}
	return T;
}

int dfs(int u,int p,int t,int b=0){
	add(u,t);
	val[u] = t;
	if(t == T){
		t = 0;
		add(u,t);
	}
	// cerr << u << '\n';
	vector<int> to;
	for(int v : g[u]){
		if(v == p)continue;
		to.push_back(v);
	}
	for(int i=0;i<(int)to.size()-1;i++){
		// if(u == 3)cerr << to.size() << '\n';
		int v = to[i];
		t = dfs(v,u,t+1,0) + 1;
		add(u,t);
		if(t == T){
			t = best(u,t);
			add(u,t);
		}
	}
	for(int i=T-1;i>=0;i--){
		if(check.find({u,i}) == check.end()){
			if(check.find({p,i+1}) == check.end() && i >= b-1){
				if(to.size()){
					// cerr << u << ' ' << to.back() << ' ' << i << '\n';
					t = dfs(to.back(),u,t+1,i) + 1;
					// cerr << t << '\n';
					add(u,t);
					if(t == T){
						t = best(u,t);
						add(u,t);
					}
				}
				if(t != i && p){
					assert(t > i);
					add(u,i);
				}
				return i;
			}
		}
	}
	if(p && t+1 <= T && check.find({p,t+1}) == check.end()){
		if(to.size()){
			// cerr << t << ' ' << u << ' ' << to.back() << '\n';
			t = dfs(to.back(),u,t+1,t+1) + 1;
			if(t == T){
				t = best(u,t);
				add(u,t);
			}
			add(u,t);
		}
		return t;
	}
	// if(p)
	// 	assert(0);
	return 0;
}

int main(){
	fast
	int n;
	cin>>n;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=n;i++)T = max(T , (int)g[i].size());
	val[1]=1;
	dfs(1,0,0);
	// assert(check.size() == m.size());
	cout << m.size() << '\n';
	for(pair<int,int> i : m){
		cout << i.first << ' ' << i.second << '\n';
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
mt19937_64 rngll(rng());
const int mn=1e5+10;
const ll mod=1e9+7;
vector<int>g[mn];
vector<pii>ans;
int mo=-1;
void dfs(int x,int p,int com){
    ans.push_back({x,com});
    vector<int>np;
    for(int y:g[x])if(y!=p)np.push_back(y);
    int cur=com;
    for(int i=0;i<np.size();i++){
        cur++;
        if(cur>mo){
            ans.push_back({x,com-(np.size()-i)-1});
            cur=com-(np.size()-i);
        }
        dfs(np[i],x,cur);
        ans.push_back({x,cur});
    }
    if(cur!=com-1)ans.push_back({x,com-1});
}
int main() {
    int n;
    scanf("%d",&n);
    for(int i=0;i<n-1;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i=1;i<=n;i++)mo=max(mo,(int)g[i].size());
    ans.push_back({1,0});
    int cur=0;
    for(int y:g[1])dfs(y,1,++cur),ans.push_back({1,cur});
    printf("%d\n",ans.size());
    for(pii p:ans)printf("%d %d\n",p.first,p.second);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
mt19937_64 rngll(rng());
const int mn=1e5+10;
const ll mod=1e9+7;
vector<int>g[mn];
vector<pii>ans;
int mo=-1;
void dfs(int x,int p,int com){
    ans.push_back({x,com});
    vector<int>np;
    for(int y:g[x])if(y!=p)np.push_back(y);
    int cur=com;
    for(int i=0;i<np.size();i++){
        cur++;
        if(cur>mo){
            ans.push_back({x,0});
            cur=1;
        }
        dfs(np[i],x,cur);
        ans.push_back({x,cur});
    }
    if(cur!=com-1)ans.push_back({x,com-1});
}
int main() {
    int n;
    scanf("%d",&n);
    for(int i=0;i<n-1;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i=1;i<=n;i++)mo=max(mo,(int)g[i].size());
    ans.push_back({1,0});
    int cur=0;
    for(int y:g[1])dfs(y,1,++cur),ans.push_back({1,cur});
    printf("%d\n",ans.size());
    for(pii p:ans)printf("%d %d\n",p.first,p.second);
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <algorithm>
#include <cassert>
#include <random>
#include <iomanip>
#include <bitset>
#define FOR(i, n, m) for(ll i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-9;

template<typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
    os << to_string(p.first) << " " << to_string(p.second);
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
    REP(i, v.size()) {
        if(i) os << " ";
        os << v[i];
    }
    return os;
}

struct modint {
    ll n;
public:
    modint(const ll n = 0) : n((n % mod + mod) % mod) {}
    static modint pow(modint a, int m) {
        modint r = 1;
        while(m > 0) {
            if(m & 1) { r *= a; }
            a = (a * a); m /= 2;
        }
        return r;
    }
    modint &operator++() { *this += 1; return *this; }
    modint &operator--() { *this -= 1; return *this; }
    modint operator++(int) { modint ret = *this; *this += 1; return ret; }
    modint operator--(int) { modint ret = *this; *this -= 1; return ret; }
    modint operator~() const { return (this -> pow(n, mod - 2)); } // inverse
    friend bool operator==(const modint& lhs, const modint& rhs) {
        return lhs.n == rhs.n;
    }
    friend bool operator<(const modint& lhs, const modint& rhs) {
        return lhs.n < rhs.n;
    }
    friend bool operator>(const modint& lhs, const modint& rhs) {
        return lhs.n > rhs.n;
    }
    friend modint &operator+=(modint& lhs, const modint& rhs) {
        lhs.n += rhs.n;
        if (lhs.n >= mod) lhs.n -= mod;
        return lhs;
    }
    friend modint &operator-=(modint& lhs, const modint& rhs) {
        lhs.n -= rhs.n;
        if (lhs.n < 0) lhs.n += mod;
        return lhs;
    }
    friend modint &operator*=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * rhs.n) % mod;
        return lhs;
    }
    friend modint &operator/=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * (~rhs).n) % mod;
        return lhs;
    }
    friend modint operator+(const modint& lhs, const modint& rhs) {
        return modint(lhs.n + rhs.n);
    }
    friend modint operator-(const modint& lhs, const modint& rhs) {
        return modint(lhs.n - rhs.n);
    }
    friend modint operator*(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * rhs.n);
    }
    friend modint operator/(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * (~rhs).n);
    }
};
istream& operator>>(istream& is, modint m) { is >> m.n; return is; }
ostream& operator<<(ostream& os, modint m) { os << m.n; return os; }

#define MAX_N 1010101
long long extgcd(long long a, long long b, long long& x, long long& y) {
    long long d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}
long long mod_inverse(long long a, long long m) {
    long long x, y;
    if(extgcd(a, m, x, y) == 1) return (m + x % m) % m;
    else return -1;
}
vector<long long> fact(MAX_N+1, inf);
long long mod_fact(long long n, long long& e) {
    if(fact[0] == inf) {
        fact[0]=1;
        if(MAX_N != 0) fact[1]=1;
        for(ll i = 2; i <= MAX_N; ++i) {
            fact[i] = (fact[i-1] * i) % mod;
        }
    }
    e = 0;
    if(n == 0) return 1;
    long long res = mod_fact(n / mod, e);
    e += n / mod;
    if((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod;
    return (res * fact[n % mod]) % mod;
}
// return nCk
long long mod_comb(long long n, long long k) {
    if(n < 0 || k < 0 || n < k) return 0;
    long long e1, e2, e3;
    long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2), a3 = mod_fact(n - k, e3);
    if(e1 > e2 + e3) return 0;
    return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod;
}

using mi = modint;

mi mod_pow(mi a, ll n) {
    mi ret = 1;
    mi tmp = a;
    while(n > 0) {
        if(n % 2) ret *= tmp;
        tmp = tmp * tmp;
        n /= 2;
    }
    return ret;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

int n, roop;
vector<vector<int>> t;
vector<pair<int, int>> ans;

void dfs(int v, int p, int tm) {
    ans.pb({v + 1, tm});
    int ch = (int)t[v].size();
    if(p != -1) ch--;
    int tmp = tm;
    for(auto adj: t[v]) {
        if(adj == p) continue;
        if(tmp == roop) {
            ans.pb({v + 1, tm - 1 - ch});
            tmp = tm - 1 - ch;
        }
        dfs(adj, v, tmp + 1);
        ans.pb({v + 1, tmp + 1});
        ch--;
        tmp++;
    }
    if(tmp >= tm && p != -1) ans.pb({v + 1, tm - 1});
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    t.resize(n);
    REP(i, n - 1) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        t[u].pb(v);
        t[v].pb(u);
    }
    REP(i, n) roop = max(roop, (int)t[i].size());
    dfs(0, -1, 0);
    cout << (int)ans.size() << "\n";
    REP(i, ans.size()) {
        cout << ans[i].first << " " << ans[i].second << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <algorithm>
#include <cassert>
#include <random>
#include <iomanip>
#include <bitset>
#define FOR(i, n, m) for(ll i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-9;

template<typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
    os << to_string(p.first) << " " << to_string(p.second);
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
    REP(i, v.size()) {
        if(i) os << " ";
        os << v[i];
    }
    return os;
}

struct modint {
    ll n;
public:
    modint(const ll n = 0) : n((n % mod + mod) % mod) {}
    static modint pow(modint a, int m) {
        modint r = 1;
        while(m > 0) {
            if(m & 1) { r *= a; }
            a = (a * a); m /= 2;
        }
        return r;
    }
    modint &operator++() { *this += 1; return *this; }
    modint &operator--() { *this -= 1; return *this; }
    modint operator++(int) { modint ret = *this; *this += 1; return ret; }
    modint operator--(int) { modint ret = *this; *this -= 1; return ret; }
    modint operator~() const { return (this -> pow(n, mod - 2)); } // inverse
    friend bool operator==(const modint& lhs, const modint& rhs) {
        return lhs.n == rhs.n;
    }
    friend bool operator<(const modint& lhs, const modint& rhs) {
        return lhs.n < rhs.n;
    }
    friend bool operator>(const modint& lhs, const modint& rhs) {
        return lhs.n > rhs.n;
    }
    friend modint &operator+=(modint& lhs, const modint& rhs) {
        lhs.n += rhs.n;
        if (lhs.n >= mod) lhs.n -= mod;
        return lhs;
    }
    friend modint &operator-=(modint& lhs, const modint& rhs) {
        lhs.n -= rhs.n;
        if (lhs.n < 0) lhs.n += mod;
        return lhs;
    }
    friend modint &operator*=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * rhs.n) % mod;
        return lhs;
    }
    friend modint &operator/=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * (~rhs).n) % mod;
        return lhs;
    }
    friend modint operator+(const modint& lhs, const modint& rhs) {
        return modint(lhs.n + rhs.n);
    }
    friend modint operator-(const modint& lhs, const modint& rhs) {
        return modint(lhs.n - rhs.n);
    }
    friend modint operator*(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * rhs.n);
    }
    friend modint operator/(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * (~rhs).n);
    }
};
istream& operator>>(istream& is, modint m) { is >> m.n; return is; }
ostream& operator<<(ostream& os, modint m) { os << m.n; return os; }

#define MAX_N 1010101
long long extgcd(long long a, long long b, long long& x, long long& y) {
    long long d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}
long long mod_inverse(long long a, long long m) {
    long long x, y;
    if(extgcd(a, m, x, y) == 1) return (m + x % m) % m;
    else return -1;
}
vector<long long> fact(MAX_N+1, inf);
long long mod_fact(long long n, long long& e) {
    if(fact[0] == inf) {
        fact[0]=1;
        if(MAX_N != 0) fact[1]=1;
        for(ll i = 2; i <= MAX_N; ++i) {
            fact[i] = (fact[i-1] * i) % mod;
        }
    }
    e = 0;
    if(n == 0) return 1;
    long long res = mod_fact(n / mod, e);
    e += n / mod;
    if((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod;
    return (res * fact[n % mod]) % mod;
}
// return nCk
long long mod_comb(long long n, long long k) {
    if(n < 0 || k < 0 || n < k) return 0;
    long long e1, e2, e3;
    long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2), a3 = mod_fact(n - k, e3);
    if(e1 > e2 + e3) return 0;
    return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod;
}

using mi = modint;

mi mod_pow(mi a, ll n) {
    mi ret = 1;
    mi tmp = a;
    while(n > 0) {
        if(n % 2) ret *= tmp;
        tmp = tmp * tmp;
        n /= 2;
    }
    return ret;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

int n, roop;
vector<vector<int>> t;
vector<pair<int, int>> ans;

void dfs(int v, int p, int tm) {
    ans.pb({v + 1, tm});
    int ch = (int)t[v].size();
    if(p != -1) ch--;
    if(ch == 0) {
        ans.pb({v + 1, tm - 1});
        return;
    }
    int tmp = tm;
    for(auto adj: t[v]) {
        if(adj == p) continue;
        if(tmp == roop) {
            ans.pb({v + 1, tm - 1 - ch});
            tmp = tm - 1 - ch;
        }
        dfs(adj, v, tmp + 1);
        ans.pb({v + 1, tmp + 1});
        ch--;
        tmp++;
    }
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    t.resize(n);
    REP(i, n - 1) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        t[u].pb(v);
        t[v].pb(u);
    }
    REP(i, n) roop = max(roop, (int)t[i].size());
    dfs(0, -1, 0);
    cout << (int)ans.size() << "\n";
    REP(i, ans.size()) {
        cout << ans[i].first << " " << ans[i].second << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <algorithm>
#include <cassert>
#include <random>
#include <iomanip>
#include <bitset>
#define FOR(i, n, m) for(ll i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-9;

template<typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
    os << to_string(p.first) << " " << to_string(p.second);
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
    REP(i, v.size()) {
        if(i) os << " ";
        os << v[i];
    }
    return os;
}

struct modint {
    ll n;
public:
    modint(const ll n = 0) : n((n % mod + mod) % mod) {}
    static modint pow(modint a, int m) {
        modint r = 1;
        while(m > 0) {
            if(m & 1) { r *= a; }
            a = (a * a); m /= 2;
        }
        return r;
    }
    modint &operator++() { *this += 1; return *this; }
    modint &operator--() { *this -= 1; return *this; }
    modint operator++(int) { modint ret = *this; *this += 1; return ret; }
    modint operator--(int) { modint ret = *this; *this -= 1; return ret; }
    modint operator~() const { return (this -> pow(n, mod - 2)); } // inverse
    friend bool operator==(const modint& lhs, const modint& rhs) {
        return lhs.n == rhs.n;
    }
    friend bool operator<(const modint& lhs, const modint& rhs) {
        return lhs.n < rhs.n;
    }
    friend bool operator>(const modint& lhs, const modint& rhs) {
        return lhs.n > rhs.n;
    }
    friend modint &operator+=(modint& lhs, const modint& rhs) {
        lhs.n += rhs.n;
        if (lhs.n >= mod) lhs.n -= mod;
        return lhs;
    }
    friend modint &operator-=(modint& lhs, const modint& rhs) {
        lhs.n -= rhs.n;
        if (lhs.n < 0) lhs.n += mod;
        return lhs;
    }
    friend modint &operator*=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * rhs.n) % mod;
        return lhs;
    }
    friend modint &operator/=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * (~rhs).n) % mod;
        return lhs;
    }
    friend modint operator+(const modint& lhs, const modint& rhs) {
        return modint(lhs.n + rhs.n);
    }
    friend modint operator-(const modint& lhs, const modint& rhs) {
        return modint(lhs.n - rhs.n);
    }
    friend modint operator*(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * rhs.n);
    }
    friend modint operator/(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * (~rhs).n);
    }
};
istream& operator>>(istream& is, modint m) { is >> m.n; return is; }
ostream& operator<<(ostream& os, modint m) { os << m.n; return os; }

#define MAX_N 1010101
long long extgcd(long long a, long long b, long long& x, long long& y) {
    long long d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}
long long mod_inverse(long long a, long long m) {
    long long x, y;
    if(extgcd(a, m, x, y) == 1) return (m + x % m) % m;
    else return -1;
}
vector<long long> fact(MAX_N+1, inf);
long long mod_fact(long long n, long long& e) {
    if(fact[0] == inf) {
        fact[0]=1;
        if(MAX_N != 0) fact[1]=1;
        for(ll i = 2; i <= MAX_N; ++i) {
            fact[i] = (fact[i-1] * i) % mod;
        }
    }
    e = 0;
    if(n == 0) return 1;
    long long res = mod_fact(n / mod, e);
    e += n / mod;
    if((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod;
    return (res * fact[n % mod]) % mod;
}
// return nCk
long long mod_comb(long long n, long long k) {
    if(n < 0 || k < 0 || n < k) return 0;
    long long e1, e2, e3;
    long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2), a3 = mod_fact(n - k, e3);
    if(e1 > e2 + e3) return 0;
    return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod;
}

using mi = modint;

mi mod_pow(mi a, ll n) {
    mi ret = 1;
    mi tmp = a;
    while(n > 0) {
        if(n % 2) ret *= tmp;
        tmp = tmp * tmp;
        n /= 2;
    }
    return ret;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

int n, roop;
vector<vector<int>> t;
vector<pair<int, int>> ans;

void dfs(int v, int p, int tm) {
    ans.pb({v + 1, tm});
    int ch = (int)t[v].size();
    if(p != -1) ch--;
    int tmp = tm;
    for(auto adj: t[v]) {
        if(adj == p) continue;
        if(tmp == roop) {
            ans.pb({v + 1, tm - 1 - ch});
            tmp = tm - 1 - ch;
        }
        dfs(adj, v, tmp + 1);
        ans.pb({v + 1, tmp + 1});
        ch--;
        tmp++;
    }
    if(tmp >= tm) ans.pb({v + 1, tm - 1});
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    t.resize(n);
    REP(i, n - 1) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        t[u].pb(v);
        t[v].pb(u);
    }
    REP(i, n) roop = max(roop, (int)t[i].size());
    dfs(0, -1, 0);
    cout << (int)ans.size() << "\n";
    REP(i, ans.size()) {
        cout << ans[i].first << " " << ans[i].second << "\n";
    }
    return 0;
}


#include<stdio.h>
int main()
{
	long int i,n,a,b,ansa=0,ansb=0;
	scanf("%ld",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%ld",&a);
		ansa=ansa|a;
	}
	for(i=1;i<=n;i++)
	{
		scanf("%ld",&b);
		ansb=ansb|b;
	}
	printf("%ld\n",ansa+ansb);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
int main(){
	int n,i,j;
	scanf("%d",&n);
	long long a[n],b[n];
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	long long f1=0,f2=0,sum=0;
	for(i=0;i<n;i++){
		f1=f1|a[i];
		f2=f2|b[i];
	}
	sum=f1+f2;
	printf("%lld\n",sum);
}


#include<stdio.h>
#include<math.h>
int main()
{
    int n;
    int i,j,a,b,sum1=0,sum2=0;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d",&a);
        sum1=sum1|a;
    }
    for(j=0;j<n;j++){
        scanf("%d",&b);
        sum2=sum2|b;
    }
    printf("%d\n",sum1+sum2);
    return 0;
}


#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    long long sum = 0;
    for (int j = 0; j < 2; j++) {
        int r = 0, t;
        for (int i = 0; i < n; i++) {
            scanf("%d", &t);
            r |= t;
        }
        sum += r;
    }
    printf("%I64d", sum);
}


#include<stdio.h>
#include<math.h>
#include<string.h>

typedef long int tep;

int main()
{
    tep n,a[10005],b[10005],i,j,max,k,l,sum1,sum2,m;
    scanf("%ld",&n);

    for(i=0 ; i<n ; i++)
        scanf("%ld",&a[i]);
    for(j=0 ; j<n ; j++)
        scanf("%ld",&b[j]);
    max=0;
    for(i=0 ; i<n ; i++)
    {
        for(j=i ; j<n ; j++)
        {
            m=j+1;sum1=0;sum2=0;
            for(k=i ; k<m ; k++)
            {
                sum1=sum1 | a[k];
                sum2=sum2 | b[k];
            }
            if( (sum1+sum2)>max)
                max=(sum1+sum2);
        }
    }

    printf("%ld\n",max);

    return 0;
}


//#include <cstdio>
//#define Tmax 1001;
//using namespace std;
//int N;
//long a, b, R1 = 0, R2 = 0;
//int main()
//{
//    //freopen("txt.in","r", stdin);
//    scanf("%d", &N);
//    for(int i = 0; i < N; scanf("%ld", &a), R1 |= a, i++);
//    for(int i = 0; i < N; scanf("%ld", &b), R2 |= b, i++);
//    printf("%ld", R1+R2);
//    return 0;
//}


#include <stdio.h>
#define Tmax 1001;
int N;
long a, b, R1 = 0, R2 = 0;
int main()
{
    scanf("%d", &N);
    for(int i = 0; i < N; scanf("%ld", &a), R1 |= a, i++);
    for(int i = 0; i < N; scanf("%ld", &b), R2 |= b, i++);
    printf("%ld", R1+R2);
    return 0;
}


#include <cstdio>
#define Tmax 1001;
using namespace std;
int N;
long a, b, R1 = 0, R2 = 0;
int main()
{
    //freopen("txt.in","r", stdin);
    scanf("%d", &N);
    for(int i = 0; i < N; scanf("%ld", &a), R1 |= a, i++);
    for(int i = 0; i < N; scanf("%ld", &b), R2 |= b, i++);
    printf("%ld", R1+R2);
    return 0;
}


#include <stdio.h>
#include <limits.h>
long long int operate(long long int *arr, long long int l,long long int r);
int main(){
 long long int store[1000], temp ;
long long int n,i,j,k, highest=INT_MIN;
scanf("%lli", &n);
i=n,j=n;
long long int a[1000]; long long int b[1000];
while(i)scanf("%lli", &a[n-(i--)]);
while(j)scanf("%lli", &b[n-(j--)]);
i=n,j=n;

int l=0,r=n-1;
while(l<r){
    temp = operate(a,l,r) + operate(b,l,r); l++;r--;
    if(temp>highest) highest=temp;

}
printf("%d\n", highest);
return 0;
}

long long int operate(long long int *arr,long long int l, long long r){
 long long int initial = 0;
 for(l=l;l<=r;l++) initial=initial | arr[l];
 return initial;

}


#include <stdio.h>
#include <limits.h>
long long int operate(long long int *arr, long long int l,long long int r);
int main(){
 long long int store[1000], temp ;
long long int n,i,j,k, highest=INT_MIN;
scanf("%lli", &n);
i=n,j=n;
long long int a[1000]; long long int b[1000];
while(i)scanf("%lli", &a[n-(i--)]);
while(j)scanf("%lli", &b[n-(j--)]);
i=n,j=n;

int l=0,r=n-1;
while(l!=r){
    temp = operate(a,l,r) + operate(b,l,r); l++;r--;
    if(temp>highest) highest=temp;

}
printf("%d\n", highest);
return 0;
}

long long int operate(long long int *arr,long long int l, long long r){
 long long int initial = 0;
 for(l=l;l<=r;l++) initial=initial | arr[l];
 return initial;

}


#include <stdio.h>
#include <limits.h>
long long int operate(long long int *arr, long long int l,long long int r);
int main(){
 long long int store[1000], temp ;
long long int n,i,j,k, highest=INT_MIN;
scanf("%lli", &n);
i=n,j=n;
long long int a[1000]; long long int b[1000];
while(i)scanf("%lli", &a[n-(i--)]);
while(j)scanf("%lli", &b[n-(j--)]);
i=n,j=n;

int l=0,r=n-1;
while(l<=r){
    temp = operate(a,l,r) + operate(b,l,r); l++;r--;
   // printf("%lld\n", temp);
    if(temp>highest) highest=temp;

}
printf("%lld\n", highest);
return 0;
}

long long int operate(long long int *arr,long long int l, long long r){
 long long int initial = 0;
 for(l=l;l<=r;l++) initial=initial | arr[l];
 return initial;

}


#include<iostream>
#include<algorithm>

using namespace std;

int func(long long *x,int l,int r);

int main()
{
    int x,y,n,l,r,k=0;
    long long a[1001],b[1001],temp,high=0;
    cin>>n;
    for(x=0;x<n;x++)cin>>a[x];
    for(y=0;y<n;y++)cin>>b[y];
    for(l=0;l<n;l++)
    {
        for(r=n;l<=r;r--)
        {
            temp=func(a,l,r)+func(b,l,r);
            if(temp>high)high=temp;
            else temp=temp;
        }
    }
    cout<<high<<endl;
}

int func(long long *x,int l,int r)
{
    int i,OR=0,a[1001];
    for(i=l;i<=r;i++)
    {
         OR=OR|x[i];
    }
    return OR;

}


#include<stdio.h>
int main()
{
	int n,a[1005],b[1005],i,j,final,p,k,ans;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=0;i<n;i++)
		scanf("%d",&b[i]);
	for(i=0;i<n;i++)
	{
		ans1=0;
		ans2=0;
		for(j=i;j<n;j++)
		{
			ans1=ans1|a[j];
			ans2=ans2|b[j];
			ans=ans1+ans2;
			if(ans>final)
			{
				final=ans;
			}
		}
	}
	printf("%d\n",final);
	return 0;
}


#include<stdio.h>
int main()
{
	int n,a[1005],b[1005],i,j,final,p,k,ans,ans1,ans2;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=0;i<n;i++)
		scanf("%d",&b[i]);
	for(i=0;i<n;i++)
	{
		ans1=0;
		ans2=0;
		for(j=i;j<n;j++)
		{
			ans1=ans1|a[j];
			ans2=ans2|b[j];
			ans=ans1+ans2;
			if(ans>final)
			{
				final=ans;
			}
		}
	}
	printf("%d\n",final);
	return 0;
}


#include<stdio.h>

int main()
{
    int max1,max2,b[100005],sum1,temp1,flag,sum,a[100005],n,i;
    scanf("%d",&n);
    max1=-1;
    max2=-1;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(max1<a[i])
        {
            max1=a[i];
        }
    }
    temp1=1;
    for(i=0;i<n;i++)
    {
        scanf("%d",&b[i]);
        if(max2<b[i])
        {
            max2=b[i];
        }
    }
    while(temp1<max1)
    {
        temp1=temp1*2;
    }
    sum=0;
    while(temp1>0)
    {
        flag=0;
        for(i=0;i<n;i++)
        {
            if(a[i]/temp1>=1 && flag==0)
            {
                sum=sum+temp1;
                flag=1;
                a[i]=a[i]-temp1;
            }
            else if(flag==1 && a[i]/temp1>=1)
            {
                a[i]=a[i]-temp1;
            }
        }
        temp1=temp1/2;
    }
    sum1=sum;
    temp1=1;
    while(temp1<max2)
    {
        temp1=temp1*2;
    }
    sum=0;
    while(temp1>0)
    {
        flag=0;
        for(i=0;i<n;i++)
        {
            if(b[i]/temp1>=1 && flag==0)
            {
                sum=sum+temp1;
                flag=1;
                b[i]=b[i]-temp1;
            }
            else if(flag==1 && b[i]/temp1>=1)
            {
                b[i]=b[i]-temp1;
            }
        }
        temp1=temp1/2;
    }
    sum=sum1+sum;
    printf("%d",sum);
    return 0;
}


#include<stdio.h>

int main() {

long long int n, x = 0, y = 0, d, i;
scanf("%lld",&n);
for(i=0;i<n;i++){
    scanf("%lld",&d);
    x|=d;
}
for(i=0;i<n;i++){
    scanf("%lld",&d);
    y|=d;
}
printf("%lld",x+y);
return 0;
}


#include<stdio.h>
int main()
{
long long int n,a=0,b=0,d,i;
scanf("%lld",&n);
for(i=0;i<n;i++){scanf("%lld",&d);a|=d;}
for(i=0;i<n;i++){scanf("%lld",&d);b|=d;}
printf("%lld",a+b);
return 0 ;
}


#include<stdio.h>
int main()
{
long long int n,a=0,b=0,d,i;
scanf("%lld",&n);
for(i=0;i<n;i++){scanf("%lld",&d);a|=d;}
for(i=0;i<n;i++){scanf("%lld",&d);b|=d;}
printf("%lld",a+b);
return 0;
}


#include<stdio.h>
int main()
{
	long int mx=0,mx1,mx2,mx3=0, a[1005],b[1005];
	int i,j,k,n;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%ld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%ld",&b[i]);
	mx2=0,mx1=0;
	long long int s=0;
	for(i=0;i<n-1;i++)
	{
		mx=mx | a[i];
		mx3=mx3|b[i];
		//printf("%ld\n",mx);
		for(j=i+1;j<n;j++)
		{
		 	mx= mx | a[j];
		 	mx3=mx3|b[j];
			if(mx2<mx)
			 	mx2=mx;
			if(mx1<mx3)
				mx1=mx3;
			if(s<mx1+mx2)
				s=mx1+mx2;
		}
	}
	printf("%lld\n",s);
	return 0;
}


#include<stdio.h>
int main()
{
	long int mx=0,mx1,mx2, a[1005],b[1005];
	int i,j,k,n;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%ld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%ld",&b[i]);
	mx2=0;
	for(i=0;i<n;i++)
	{
		mx=mx | a[i];
		//printf("%ld\n",mx);
		for(j=i+1;j<n;j++)
		{
		 	mx= mx | a[j];
			 if(mx2<mx)
			 	mx2=mx;
		}
	}
	mx=0,mx1=0;
	for(i=0;i<n;i++)
	{
		mx=mx | b[i];
		//printf("%ld\n",mx);
		for(j=i+1;j<n;j++)
		{
		 	mx= mx | a[j];
			 if(mx1<mx)
			 	mx1=mx;
		}
	}
	printf("%lld\n",mx1+mx2);
	return 0;
}


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n,i;
    scanf("%d",&n);
    long long ans1 = 0;
    long long ans2 = 0;
    for(i=0;i<n;i++){
        long long a;
        scanf("%lld",&a);
        ans1|=a;
    }
    for(i=0;i<n;i++){
        long long b;
        scanf("%lld",&b);
        ans2|=b;
    }
    printf("%lld",ans1+ans2);
    return 0;
}


#include<stdio.h>
int main()
{

	long long int n,i,tmp=0,max=0,max1=0,ans;
	scanf("%lld",&n);
	for(i=0;i<n;i++){
		scanf("%lld",&tmp);
		max=max|tmp;
	}
	for(i=0;i<n;i++){
		scanf("%lld",&tmp);
		max1=max1|tmp;
	}
	ans=max+max1;
	printf("%lld\n",ans);
	return 0;
}


#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int m1=0,m2=0;
    int i;
    for(i=0;i<n;i++)
    {

        int x;
        scanf("%d",&x);
        m1=m1|x;
    }
     for(i=0;i<n;i++)
    {

        int x;
        scanf("%d",&x);
        m2=m2|x;
    }
    printf("%d\n",m1+m2);
    return 0;
}


#include <stdio.h>

int main()
{
    int i,n;
    long a[1001],b[1001];
    scanf("%d",&n);
    for (i=0; i<n; i++){
        scanf("%d",&a[i]);
    }
    for (i=0; i<n; i++){
        scanf("%d",&b[i]);
    }

    long s=0;
    long maxs=-1000;
    for (i=1; i<n-2; i++){
        s=a[i]+a[i+1];
        if (s>maxs) maxs=s;
        s=0;
    }
    long ans=maxs;
    maxs=-1000;
    for (i=1; i<n-2; i++){
        s=b[i]+b[i+1];
        if (s>maxs) maxs=s;
        s=0;
    }
    ans+=maxs;
    printf("%d",ans);
    return 0;
}


#include<stdio.h>
int main()
{
    int n,a[1001],b[1001],current1,current2,i,j,ans=0,current,p,k,cu1,cu2;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
        scanf("%d",&b[i]);
    for(i=0;i<n;i++)
    {
        current1=0;
        current2=0;
        for(j=i;j<n;j++)
        {
            current1=current1|a[j];
            current2=current2|b[j];
            current=current1+current2;
            if(current>ans)
            {
                ans=current;
                p=i;
                k=j;
            }
        }
    }
    for(i=0;i<9;i++)
    {
        cu1=cu1^a[i];
        cu2=cu2^b[i];
    }
    printf("%d\n",ans);
    return 0;
}


#include<stdio.h>
int main()
{
	int i,n,j,k=0,l,x,y;
	int max=0;
	scanf("%d",&n);
	int a[n],b[n];
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=0;i<n;i++)
		scanf("%d",&b[i]);
	for(i=0;i<n;i++)
	{
		k=a[i];
		l=b[i];
		for(j=i+1;j<n;j++)
		{
			k=a[j]|k;
			l=l|b[j];
			if((k+l)>max)
			{
				max=k+l;
				x=i;
				y=j;
			}
		}
	}
	printf("%d   ",max);
	return 0;
}


#include<stdio.h>

int main()
{  int n;
 scanf("%d",&n);
    int a[n],b[n];
      int i,j,p=0,q=0;
      for(i=0;i<n;i++)
        {
		scanf("%d",&a[i]);
		 p|=a[i];
	}
         for(j=0;j<n;j++)
     {
		         scanf("%d",&b[i]); q|=b[i];
		     }
		     printf("%d\n",p+q);
		     return 0;

}


// Round 344 (Div. 2)
// A. Interview
#include <stdio.h>
#define MAX 1005

// prototypes
void readArray(int *v, int n);
int f(int *v, int l, int r);
int max(int n1, int n2);

int main()
{
    int left, right, n, a[MAX], b[MAX], best=0;

    scanf("%d", &n);
    readArray(a,n);
    readArray(b,n);

    for(left=1; left<=n; left++)
    {
	for(right=left; right<=n; right++)
	    best = max( best, f(a,left,right) + f(b,left,right) );
    }

    printf("%d\n", best);

    return 0;
}

void readArray(int *v, int n)
{
    int i;
    for(i=1; i<=n; i++)
	scanf("%d", (v+i));
}

int f(int *v, int l, int r)
{
    int i, sum=0;
    for(i=l; i<=r; i++)
	sum |= *(v+i);

    return sum;
}

int max(int n1, int n2)
{
    return (n1 > n2) ? n1 : n2;
}


// Round 344 (Div. 2)
// A. Interview
#include <stdio.h>
#define MAX 1005

// prototypes
void readArray(int *v, int n);
int f(int *v, int l, int r);
int max(int n1, int n2);

int main()
{
    int left, right, n, a[MAX], b[MAX], best=0;

    scanf("%d", &n);
    readArray(a,n);
    readArray(b,n);

    for(left=1; left<=n; left++)
    {
	for(right=left; right<=n; right++)
	    best = max( best, f(a,left,right) + f(b,left,right) );
    }

    printf("%d\n", best);

    return 0;
}

void readArray(int *v, int n)
{
    int i;
    for(i=0; i<n; i++)
	scanf("%d", (v+i));
}

int f(int *v, int l, int r)
{
    int i, sum=0;
    for(i=l; i<=r; i++)
	sum |= *(v+i);

    return sum;
}

int max(int n1, int n2)
{
    return (n1 > n2) ? n1 : n2;
}


// Round 344 (Div. 2)
// A. Interview
#include <stdio.h>
#define MAX 1005

// prototypes
void readArray(int *v, int n);
int f(int *v, int l, int r);

int main()
{
    int n, a[MAX], b[MAX];

    scanf("%d", &n);
    readArray(a,n);
    readArray(b,n);

    // the maximum value is always the whole sequence
    // because of this property:
    // x OR y >= x
    printf("%d\n", f(a,1,n) + f(b,1,n));

    return 0;
}

void readArray(int *v, int n)
{
    int i;
    for(i=1; i<=n; i++)
	scanf("%d", (v+i));
}

int f(int *v, int l, int r)
{
    int i, sum=0;
    for(i=l; i<=r; i++)
	sum |= *(v+i);

    return sum;
}


#include <stdio.h>

int main(){
	int n;
	scanf("%d",&n);
	int i,a,b,temp;
	scanf("%d",&temp);
	a=temp;
	for (i=1;i<n;i++){
		scanf("%d",&temp);
		a=a|temp;
	}
	scanf("%d",&temp);
	b=temp;
	for (i=1;i<n;i++){
		scanf("%d",&temp);
		b=b|temp;
	}
	long int sum=a+b;
	printf("%ld",sum);
	return 0;
}


#include<stdio.h>

int main()
{
	int n,i;
	scanf("%d",&n);

	long long int A[10005],B[10005];
	long long int a1=0,b1=0;
	for(i=0;i<n;i++)
	{
		scanf("%lld",&A[i]);
		a1 = a1|A[i];
	}
	for(i=0;i<n;i++)
	{
		scanf("%lld",&B[i]);
		b1 = b1|B[i];
	}

	printf("%ld\n",a1+b1);

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void){
    int size, i;
    scanf("%d", &size);

    int* bits1 = malloc(sizeof(int) * 31);
    int* bits2 = malloc(sizeof(int) * 31);
    for(i = 0; i < 31; i++){
        bits1[i] = 0;
        bits2[i] = 0;
    }

    for(i = 0; i < size; i++){
        int n;
        scanf("%d", &n);
        while(n > 0){
            bits1[(int)log2(n)] = 1;
            n -= pow(2, (int)log2(n));
        }
    }

    for(i = 0; i < size; i++){
        int n;
        scanf("%d", &n);
        while(n > 0){
            bits2[(int)log2(n)] = 1;
            n -= pow(2, (int)log2(n));
        }
    }

    long long int total = 0;
    for(i = 0; i < 31; i++){
        total += bits1[i] == 1 ? pow(2, i) : 0;
        total += bits2[i] == 1 ? pow(2, i) : 0;
    }

    printf("%I64d\n", total);

    free(bits1);
    free(bits2);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void){
    int size, i;
    scanf("%d", &size);

    int* bits1 = malloc(sizeof(int) * 31);
    int* bits2 = malloc(sizeof(int) * 31);
    for(i = 0; i < 31; i++){
        bits1[i] = 0;
        bits2[i] = 0;
    }

    for(i = 0; i < size; i++){
        int n;
        scanf("%d", &n);
        while(n > 0){
            bits1[(int)log2(n)] = 1;
            n /= 2;
        }
    }

    for(i = 0; i < size; i++){
        int n;
        scanf("%d", &n);
        while(n > 0){
            bits2[(int)log2(n)] = 1;
            n /= 2;
        }
    }

    long long int total = 0;
    for(i = 0; i < 31; i++){
        total += bits1[i] == 1 ? pow(2, i) : 0;
        total += bits2[i] == 1 ? pow(2, i) : 0;
    }

    printf("%I64d\n", total);

    free(bits1);
    free(bits2);
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

int n;
long long A[1010], B[1010];

int main(){
    int i, j, k;
    long long x, y, z, res;

    while (scanf("%d", &n) != EOF){
        for (i = 0; i < n; i++) scanf("%lld", &A[i]);
        for (i = 0; i < n; i++) scanf("%lld", &B[i]);

        res = 0;
        for (i = 0; i < n; i++){
            x = 0, y = 0;
            for (j = i; j < n; j++){
                x |= A[j], y |= B[j];
                if ((x + y) > res) res = x + y;
            }
        }
        printf("%lld\n", res);
    }
    return 0;
}


#include<stdio.h>
#define M 1007
int main()
{
	int i,n,a[M],b[M];
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(i=0;i<n;i++)
	{
		scanf("%d",&b[i]);
	}
	int pre1[M],pre2[M];
	pre1[0]=a[0];
	pre2[0]=b[0];
	for(i=1;i<n;i++)
	{
		pre1[i] = (pre1[i-1] | a[i]);
	}
	for(i=1;i<n;i++)
	{
		pre2[i] = (pre2[i-1] | b[i]);
	}
	int max1=0,max2=0;
	for(i=0;i<n;i++)
	{
		if(pre1[i]>max1)
		max1=pre1[i];
		if(pre2[i]>max2)
		max2=pre2[i];
	}
	long long int ans=max1+max2;
	printf("%lld",ans);
	return 0;
}


#include<stdio.h>
int main()
{int n,i;
long long int a,b,x,y;
scanf("%d",&n);
a=0,b=0;
for(i=0;i<n;i++)
{scanf("%lld",&x);
a=a|x;}
for(i=0;i<n;i++)
{scanf("%lld",&y);
b=b|y;}
printf("%lld\n",a+b);
return 0;
}


#include<stdio.h>

int main()
{
	long long int n,a[1000],b[1000],i;
	scanf("%lld",&n);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	long long int sum=0,temp1,temp2,max=0,j;
	for(i=0;i<n;i++)
	{
		sum=0,temp1=temp2=a[i];
		for(j=i+1;j<n;j++)
		{
			temp1|=a[j];
			temp2|=b[j];
		}
		sum+=temp1+temp2;
		if(sum>max)
			max=sum;
	}
	printf("%lld",max);
	return 0;
}


#include<stdio.h>

int main()
{
	long long int n,a[1000],b[1000],i;
	scanf("%lld",&n);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	long long int sum=0,temp1,temp2,max=0,j;
	for(i=0;i<n;i++)
	{
		sum=0,temp1=temp2=a[i];
		for(j=i+1;j<n;j++)
		{
			temp1|=a[j];
			temp2|=b[j];
			sum=temp1+temp2;
			if(sum>max)
				max=sum;
		}

	}
	printf("%lld",max);
	return 0;
}


#include<stdio.h>

int main()
{
	long long int n,a[1000],b[1000],i;
	scanf("%lld",&n);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	long long int sum=0,temp1,temp2,max=0,j;
	temp1=a[0],temp2=b[0];
	for(i=0;i<n;i++)
	{
			temp1|=a[i];
			temp2|=b[i];
			sum=temp1+temp2;
			if(sum>max)
				max=sum;
	}
	printf("%lld",max);
	return 0;
}


#include<stdio.h>
#include<string.h>

int n;
long long a[1000+10];
long long b[1000+10];

int main()
{
      scanf("%d",&n);
      for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
      for(int i=1;i<=n;i++) scanf("%lld",&b[i]);

    long long num1=a[1],num2=b[1];
    for(int i=2;i<=n;i++)
    {
        num1=num1|a[i];
        num2=num2|b[i];
    }
    printf("%lld\n",num1+num2);
    return 0;
}


#include <stdio.h>
int main(){
    int i, n, x, y, ans1 = 0, ans2 = 0;
    scanf("%d", &n);

    for(i = 0; i < n; i++){
        scanf("%d", &x);
        ans1 |= x;
    }

    for(i = 0; i < n; i++){
        scanf("%d", &y);
        ans2 |= y;
    }
    printf("%d\n", ans1+ans2);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{

   int n,x,y;
   scanf("%d",&n);
   int a[n],b[n];

   int s,i,j,max1=0,max2=0;
   for(i=0;i<n;i++)
   {
       scanf("%d",&a[i]);
   }
   for(i=0;i<n;i++)
   {
       scanf("%d",&b[i]);
   }
   for(i=0;i<n;i++)
   {  s=a[i];
       for(j=i;j<n;j++)
       {
           s=s|a[j];

           if(s>max1)
            max1=s;
       }
    }

 for(i=0;i<n;i++)
   {  s=b[i];
       for(j=i;j<n;j++)
       {
           s=s|b[j];

           if(s>max2)
            max2=s;
       }
    }
    printf("%d",max1+max2);

}


/* Problem: 631A - Interview */
/* Solver: Gusztav Szmolik */

#include <stdio.h>

int main ()
    {
    unsigned short n;
    unsigned int fa;
    unsigned short i;
    unsigned int a;
    unsigned int fb;
    unsigned int b;

    if (scanf("%hu",&n) != 1)
        return -1;
    if (n < 1 || n > 1000)
        return -1;
    fa = 0;
    for (i = 0; i < n; i++)
        {
        if (scanf("%u",&a) != 1)
            return -1;
        if (a > 1000000000)
            return -1;
        fa |= a;
        }
    fb = 0;
    for (i = 0; i < n; i++)
        {
        if (scanf("%u",&b) != 1)
            return -1;
        if (b > 1000000000)
            return -1;
        fb |= b;
        }
    printf ("%u\n",fa+fb);
    return 0;
    }


#include <stdio.h>

int main()
{
    int n;
    scanf("%d",&n);
    int a[n];
    int b[n];
    int c=0;
    int d=0;
    int f=0;
    int p=0;
    while (d<n)
    {
        scanf("%d",&a[d]);
        p=p|a[d];
        d++;
    }
    while (c<n)
    {
        scanf("%d",&b[c]);
        f=f|b[c];
        c++;
    }
    c=p+f;

    printf("%d",c);

return 0;
}


#include<stdio.h>
int main()
{
	long long int a[1000],b[1000],n,i,j,as=0,bs=0,s=0;
	scanf("%lld",&n);
	for(i=0;i<n;i++)
	{
		scanf("%lld",&a[i]);
	}
	for(i=0;i<n;i++)
	{
		scanf("%lld",&b[i]);

	}
	for(i=0;i<n;i++)
	{
		as=as|a[i];
		bs=bs|b[i];
	}
	s=as+bs;
	printf("%lld\n",s);
	return 0;
}


#include <stdio.h>

int main(){
    int n,a[1001],b[1001];
    scanf("%d",&n);

    for (int i=0;i<n;i++)
        scanf("%d",&a[i]);

    for (int i=0;i<n;i++)
        scanf("%d",&b[i]);

    int max=0;
    for (int i=0;i<n;i++){
        int q=a[i];
        int qq=b[i];
        if (max<q+qq)
               max=qq+q;
        for (int ii=i+1;ii<n;ii++){
            q|=a[ii];
            qq|=b[ii];
            if (max<q+qq)
               max=qq+q;
        }
    }
    printf("%d",max);
}


#include <stdio.h>

int main()
{
	long long int n;
	scanf("%lld",&n);
	long long int i,a[n],b[n];
	for(i=0;i<n;i++)
		scanf("%lld",a+i);
	for(i=0;i<n;i++)
		scanf("%lld",b+i);

	long long int amax=0,bmax=0;
	for(i=0;i<n;i++)
		amax|=a[i],bmax|=b[i];

	printf("%lld\n",amax+bmax );
	return 0;
}


#include <stdio.h>

int main()
{
	long long int n;
	scanf("%lld",&n);
	long long int i,a[n],b[n];
	for(i=0;i<n;i++)
		scanf("%lld",a+i);
	for(i=0;i<n;i++)
		scanf("%lld",b+i);

	long long int j,ans = 0;
	for(i=0;i<n;i++)
	{
		long long int temp1 = a[i];
		long long int temp2 = b[i];
		for(j=i;j<n;j++)
		{
			temp1|=a[j];
			temp2|=b[j];
			if(temp1+temp2 > ans)
				ans = temp1+temp2;
		}
	}

	printf("%lld\n",ans );
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i , n ;
    long long int a , b , sumA=0 , sumB=0 ;

    scanf("%d",&n);

    for(i=0 ; i<n ; i++)
    {
        scanf("%lld",&a);
        sumA = sumA | a ;
    }

    for(i=0 ; i<n ; i++)
    {
        scanf("%lld",&b);
        sumB = sumB | b ;
    }

    printf("%lld",sumA+sumB);
    return 0;
}


#include <stdio.h>

int main()
{
	int n,i;
	scanf("%d",&n);
	int ara[10000];
	int a[10000];
		int f=0,s=0,t;
	for (i=0;i<n;i++){
		scanf("%d",&ara[i]);
		f=f|ara[i];
	}
	for (i=0;i<n;i++){
		scanf("%d",&a[i]);
		s=s|a[i];
	}

	printf("%d ",f+s);

	return 0;
}


#include <stdio.h>
#define max 1000

int main(void) {
	int n;
	scanf("%d",&n);
	int a[max];
	int b[max];
	int p[max];
	for (int i=0;i<n;++i) {
		scanf("%d",&a[i]);
	}
	for (int i=0;i<n;++i) {
		scanf("%d",&b[i]);
	}
	p[0] = a[0];
	for (int i=1;i<n;++i) {
		p[i] = p[i-1] | a[i];
	}
	int ans = p[n-1];
	p[0] = b[0];
	for (int i=1;i<n;++i) {
		p[i] = p[i-1] | b[i];
	}
	printf("%d",ans+p[n-1]);
	return 0;
}


#include<stdio.h>
int main()
{
    long long int ans1;
    long long int ans2;
    int a[1001];
    int b[1001];
    int n,i,j;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i]);
    ans1=ans1|a[i];
    }
    for(j=0;j<n;j++)
    {scanf("%d",&b[j]);
    ans2=ans2|b[j];
    }
    printf("%ld",ans1+ans2);
    return 0;
}


#include <stdio.h>
typedef long long int ll;
ll max(ll n,ll a[])
{
    ll i,x=0;
    for(i=1;i<=n;i++)
    {
        x=x|a[i];
    }
    return x;
}
int main()
{
    ll n,a[2000],b[2000],i,j;
    scanf("%I64d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=1;i<=n;i++)
    {
        scanf("%d",&b[i]);
    }
    printf("%I64d\n",max(n,a)+max(n,b));
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __linux__
#define LLD     "%lld"
#else
#define LLD     "%I64d"
#endif

typedef long long ll;

int
main()
{
	int	i, n;
	ll	t, x = 0ll, y = 0ll;

	scanf ("%d", &n);

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

		scanf (LLD, &t);
		x |= t;
	}
	for (i = 0; i < n; i++) {

		scanf (LLD, &t);
		y |= t;
	}
	printf (LLD "\n", (x+y));
}


#include <stdio.h>

int main()
{
	long long int i,j,a=0,b=0,n;
	scanf("%lld",&n);
	for(i=0;i<n;i++)
	{
		scanf("%lld",&j);
		a|=j;
	}
	for(i=0;i<n;i++)
	{
		scanf("%lld",&j);
		b|=j;
	}
	printf("%lld\n",a+b);
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, i;
    scanf("%d\n", &n);
    long a[n], b[n], sumA = 0, sumB = 0;
    for (i = 0; i < n; i++){
        scanf("%ld", &a[i]);
        sumA = sumA | a[i];
    }
    for (i = 0; i < n; i++){
        scanf("%ld", &b[i]);
        sumB = sumB | b[i];
    }
    printf("%ld\n", sumA + sumB);
    return 0;
}


#include<stdio.h>

int main()

{
    int n , i , j , m = 0 , k = 0 ;

    scanf("%d",&n);

    long long int ara1[n];

    long long int ara2[n];

    for(i=0;i<n;i++)
    {
        scanf("%I64d",&ara1[i]);
        m |= (m|ara1[i]);
    }

    for(i=0;i<n;i++)
    {
        scanf("%I64d",&ara2[i]);
        k |= (k|ara2[i]);
    }

    printf("%d\n",m+k);

    return 0;

}


#include <stdio.h>

int read(int n) {
	int x = 0, x_, i;
	for (i = 0; i < n; ++i) {
		scanf("%d", &x_);
		x |= x_;
	}
	return x;
}

int main(void) {
	//freopen("input.txt", "r", stdin);

	int n;

	scanf("%d", &n);

	printf("%d", read(n) + read(n));

	return 0;
}


#include <stdio.h>

int read(int n) {
	int x = 0, x_, i;
	for (i = 0; i < n; ++i) {
		scanf("%d", &x_);
		x |= x_;
	}
	return x;
}

int main(void) {
	freopen("input.txt", "r", stdin);

	int n;

	scanf("%d", &n);

	printf("%d", read(n) + read(n));

	return 0;
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        long[] a = new long[n];
        long[] b = new long[n];
        for (int i = 0; i < n; i++){
            a[i] = scanner.nextLong();
            b[i] = scanner.nextLong();
        }

        long sum1 = a[0];
        long sum2 = b[0];
        for (int i = 1; i < n; i++){
            sum1|=a[i];
            sum2|=b[i];
        }
        System.out.println(sum1+sum2);

    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        long[] a = new long[n];
        long[] b = new long[n];
        for (int i = 0; i < n; i++){
            a[i] = scanner.nextLong();
        }
        for (int i = 0; i < n; i++){
            b[i] = scanner.nextLong();
        }
        long sum1 = a[0];
        long sum2 = b[0];
        for (int i = 1; i < n; i++){
            sum1|=a[i];
            sum2|=b[i];
        }
        System.out.println(sum1+sum2);
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        long[] a = new long[n];
        long[] b = new long[n];
        for (int i = 0; i < n; i++){
            a[i] = scanner.nextLong();
        }
        for (int i = 0; i < n; i++){
            b[i] = scanner.nextLong();
        }
        long sum1 = a[0];
        long sum2 = b[0];
        for (int i = 1; i < n; i++){
            sum1|=a[i];
            sum2|=b[i];
        }

        System.out.println(sum1+sum2);
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long[] a = new long[n];
        long[] b = new long[n];
        for (int i = 0; i < n; i++){
            a[i] = scanner.nextLong();
        }
        for (int i = 0; i < n; i++){
            b[i] = scanner.nextLong();
        }
        long sum1 = a[0];
        long sum2 = b[0];
        for (int i = 1; i < n; i++){
            sum1|=a[i];
            sum2|=b[i];
        }
        System.out.println(sum1+sum2);
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long[] a = new long[n];
        long[] b = new long[n];
        for (int i = 0; i < n; i++){
            a[i] = scanner.nextLong();
        }
        for (int i = 0; i < n; i++){
            b[i] = scanner.nextLong();
        }
        long sum1 = a[0];
        long sum2 = b[0];
        for (int i = 1; i < n; i++){
            sum1|=a[i];
            sum2|=b[i];
        }
        System.out.println(sum1+sum2);
    }
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int[] a = readArray(sc, n);
		int[] b = readArray(sc, n);
		System.out.println(solve(a, b));

		sc.close();
	}

	static int[] readArray(Scanner sc, int size) {
		int[] result = new int[size];
		for (int i = 0; i < result.length; i++) {
			result[i] = sc.nextInt();
		}
		return result;
	}

	static int solve(int[] a, int[] b) {
		int result = -1;
		for (int l = 0; l < a.length; l++) {
			int orA = 0;
			int orB = 0;
			for (int r = l; r < a.length; r++) {
				orA |= a[r];
				orB |= b[r];

				result = Math.max(result, orA + orB);
			}
		}
		return result;
	}
}


import java.util.Scanner;

public class Interview {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[] a = new int[n], b = new int[n];
		for(int i = 0; i < n; i++) a[i] = scan.nextInt();
		for(int i = 0; i < n; i++) b[i] = scan.nextInt();
		int sumA = 0;
		for(int i = 0; i < n; i++)sumA =(a[i]|sumA);
		int sumB  = 0;
		for(int i = 0; i < n; i++)sumB =(b[i]|sumB);
//		int max = 0;
//		for(int i = 0; i < n; i++){
//			for(int j = 0; j < n; j++){
//				System.out.println(a[i] | b[j]);
//				int temp = a[i]|b[j];
//				if(temp > max)max = temp;
//			}
//		}
		System.out.println(sumA+sumB);
	}

}


import java.util.Scanner;

public class CF344A {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[] a = new int[n], b = new int[n];
		for(int i = 0; i < n; i++) a[i] = scan.nextInt();
		for(int i = 0; i < n; i++) b[i] = scan.nextInt();
		int sumA = 0;
		for(int i = 0; i < n; i++)sumA+=(a[i]|sumA);
		int sumB  = 0;
		for(int i = 0; i < n; i++)sumB+=(b[i]|sumB);
//		int max = 0;
//		for(int i = 0; i < n; i++){
//			for(int j = 0; j < n; j++){
//				System.out.println(a[i] | b[j]);
//				int temp = a[i]|b[j];
//				if(temp > max)max = temp;
//			}
//		}
		System.out.println(sumA+sumB);
	}

}


import java.util.Arrays;
import java.util.Scanner;

public class TA_344 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt();
		}

		for (int i = 0; i < n; i++) {
			b[i] = in.nextInt();
		}

		int max_a = a[0];
		int max_b = a[0];

		for (int i = 0; i < n-1; i++) {
			int current_max = a[i];
			for (int j = i+1; j < n; j++) {
				current_max |= a[j];
				if (current_max > max_a) {
					max_a = current_max;
				}
			}
		}
		for (int i = 0; i < n-1; i++) {
			int current_max = b[i];
			for (int j = i+1; j < n; j++) {
				current_max |= b[j];
				if (current_max > max_b) {
					max_b = current_max;
				}
			}
		}

		System.out.println(max_a + max_b);
	}
}


import java.util.Arrays;
import java.util.Scanner;

public class TA_344 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt();
		}

		for (int i = 0; i < n; i++) {
			b[i] = in.nextInt();
		}

		int max_a = a[0];
		int max_b = b[0];

		for (int i = 0; i < n-1; i++) {
			int current_max = a[i];
			for (int j = i+1; j < n; j++) {
				current_max |= a[j];
				if (current_max > max_a) {
					max_a = current_max;
				}
			}
		}
		for (int i = 0; i < n-1; i++) {
			int current_max = b[i];
			for (int j = i+1; j < n; j++) {
				current_max |= b[j];
				if (current_max > max_b) {
					max_b = current_max;
				}
			}
		}

		System.out.println(max_a + max_b);
	}
}


import java.util.Arrays;
import java.util.Scanner;

public class TA_344 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt();
		}

		for (int i = 0; i < n; i++) {
			b[i] = in.nextInt();
		}

		int max_a = 0;
		int max_b = 0;

		for (int i = 0; i < n-1; i++) {
			int current_max = a[i];
			for (int j = i+1; j < n; j++) {
				current_max |= a[j];
				if (current_max > max_a) {
					max_a = current_max;
				}
			}
		}
		for (int i = 0; i < n-1; i++) {
			int current_max = b[i];
			for (int j = i+1; j < n; j++) {
				current_max |= b[j];
				if (current_max > max_b) {
					max_b = current_max;
				}
			}
		}

		System.out.println(max_a + max_b);
	}
}


import java.util.*;
import java.lang.*;
public class Main
{
	public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int []a=new int[n];
        int []b=new int[n];
        for(int i=0;i<n;i++)
         {
             a[i]=sc.nextInt();
         }
        for(int i=0;i<n;i++)
         {
             b[i]=sc.nextInt();
         }

         int sum1=0,sum2=0;

        for(int i=0;i<n;i++)
        {
            sum1|=a[i];
            sum2|=b[i];
        }
        System.out.println(sum1+sum2);
	}
}


import java.util.*;
import java.io.*;
import java.math.*;
public class loser
{
    static class InputReader {
        public BufferedReader br;
        public StringTokenizer token;
        public InputReader(InputStream stream)
        {
            br=new BufferedReader(new InputStreamReader(stream),32768);
            token=null;
        }

        public String next()
        {
            while(token==null || !token.hasMoreTokens())
            {
                try
                {
                    token=new StringTokenizer(br.readLine());
                }
                catch(IOException e)
                {
                    throw new RuntimeException(e);
                }
            }
            return token.nextToken();
        }

        public int nextInt()
        {
            return Integer.parseInt(next());
        }

        public long nextLong()
        {
            return Long.parseLong(next());
        }
    }
    static class card{
        int l;
        int r;
        public card(int ch,int i)
        {
            this.l=ch;
            this.r=i;
        }
    }
    static class sort implements Comparator<card>
    {
        public int compare(card o1,card o2)
        {
            if(o1.l!=o2.l)
                return (int)(o1.l-o2.l);
            else
                return (int)(o1.r-o2.r);
        }
    }
    static void shuffle(long a[])
    {
        List<Long> l=new ArrayList<>();
        for(int i=0;i<a.length;i++)
            l.add(a[i]);
        Collections.shuffle(l);
        for(int i=0;i<a.length;i++)
            a[i]=l.get(i);
    }

    /*static long gcd(long a,long b)
    {
    if(b==0)
    return a;
    else
    return gcd(b,a%b);
    }
    static int ans1=Integer.MAX_VALUE,ans2=Integer.MAX_VALUE,ans3=Integer.MAX_VALUE,ans4=Integer.MAX_VALUE;
    static boolean v[]=new boolean[101];
    static void dfs(Integer so,Set<Integer> s[]){
    if(!v[so.intValue()])
    {
    v[so]=true;
    for(Integer h:s[so.intValue()])
    {
    if(!v[h.intValue()])
    dfs(h,s);
    }
    }
    }
    static class Print{
    public PrintWriter out;
    Print(OutputStream o)
    {
    out=new PrintWriter(o);
    }
    }
    static int CeilIndex(int A[], int l, int r, int key)
    {
    while (r - l > 1) {
    int m = l + (r - l) / 2;
    if (A[m] >= key)
    r = m;
    else
    l = m;
    }

    return r;
    }

    static int LongestIncreasingSubsequenceLength(int A[], int size)
    {
    // Add boundary case, when array size is one
    int[] tailTable = new int[size];
    int len; // always points empty slot
    tailTable[0] = A[0];
    len = 1;
    for (int i = 1; i < size; i++) {
    if (A[i] < tailTable[0])
    // new smallest value
    tailTable[0] = A[i];

    else if (A[i] > tailTable[len - 1])
    // A[i] wants to extend largest subsequence
    tailTable[len++] = A[i];

    else
    // A[i] wants to be current end candidate of an existing
    // subsequence. It will replace ceil value in tailTable
    tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i];
    }
    return len;
    }*/
    /*static int binary(int n)
    {
    int s=1;
    while(n>0)
    {
    s=s<<1;
    n--;
    }
    return s-1;
    }
    static StringBuilder bin(int i,int n)
    {
    StringBuilder s=new StringBuilder();
    while(i>0)
    {
    s.append(i%2);
    i=i/2;
    }
    while(s.length()!=n)
    {
    s.append(0);
    }
    return s.reverse();
    }*/
    public static void main(String[] args)
    {
        InputReader sc=new InputReader(System.in);
        int n=sc.nextInt();
        long or1=0;
        long or2=0;
        for(int i=0;i<n;i++)
        {
            long t=sc.nextLong();
            or1=or1|t;
        }
        for(int i=0;i<n;i++)
        {
            long t=sc.nextLong();
            or2=or2|t;
        }
        System.out.println(or2+or1);
    }
}


import java.util.*;
import java.io.*;
import java.math.*;
import java.util.regex.Matcher;
//linkedhashmap contains the entry in the same order which we are giving and not in sorted way like in hashmaps
public class CF {
    FastScanner in;
    PrintWriter out;
    final static int M=1000001;
    public ArrayList<Integer> input(int m){
        ArrayList<Integer> li= new ArrayList<>();
        for(int i=0;i<m;i++)
            li.add(in.nextInt());
        return li;
    }
    class Pair{
        int x;
        int y;
    }
    public class pair<T,V> {
        private T x;
        private V y;

        public void setY(V y) {
            this.y = y;
        }

        public V getY() {
            return y;
        }

        public void setX(T x) {
            this.x = x;
        }

        public T getX() {
            return x;
        }
    }
    Set<Long> primeFactors(long n)
    {
        // Print the number of 2s that divide n
        Set<Long> li=new HashSet<>();

        while (n%2 == 0)
        {
            li.add(2L);
            n = n/2;
        }

        // n must be odd at this point.  So we can skip one element (Note i = i +2)
        for (long i = 3; i <= Math.sqrt(n); i = i+2)
        {
            // While i divides n, print i and divide n
            while (n%i == 0)
            {
                li.add(i);
                n = n/i;
            }
        }

        // This condition is to handle the case whien n is a prime number
        // greater than 2
        if (n > 2)
            li.add(n);
        return li;
    }
    int res[];
    void sieve(){
        res=new int[10000009];

        int p[]=new int[10000009];
        for(int i=2;i<=10000000;i++){
            if(p[i]==0){
                for(int j=i+i;j<=10000000;j+=i){
                    if(p[i]==0){
                        p[j]=i;
                    }
                    res[i]=res[i-1]+i;
                }
            }
            else{
                res[i]=res[i-1]+p[i];
            }
        }
    }
    public static HashSet<String> getAllPermutations(String str) {

        HashSet<String> permutations = new HashSet<String>();

        if(str == null || str.length() == 0) {
            permutations.add("" );
            return permutations;
        }

        char first = str.charAt(0);
        String remainingString = str.substring(1);
        HashSet<String> words = getAllPermutations(remainingString);
        for(String word: words) {
            for(int i = 0; i <= word.length(); i++) {
                String prefix = word.substring(0, i);
                String suffix = word.substring(i);
                permutations.add(prefix + first + suffix);
            }
        }
        return permutations;
    }

//    static class Pair<T> {
//        T a;
//        T b;
//
//        public Pair(T a, T b) {
//            this.a = a;
//            this.b = b;
//        }
//    }
private ArrayList<Integer> adj[];
    private boolean visited[];
   // private boolean visited1[];
    private LinkedList<Integer> q;
    private int level[];
    private int f=0;
    void initialize(){
        visited=new boolean[20001];
        //visited1=new boolean[10001];
        level =new int[20001];
        Arrays.fill(visited,false);
    }
    void solve() {
        int n =in.nextInt();
        int a[]=new int[n];
        int b[]=new int[n];
        for(int i=0;i<n;i++)
            a[i]=in.nextInt();
        for(int i=0;i<n;i++)
            b[i]=in.nextInt();
        BigInteger ans=BigInteger.valueOf(a[0]);
        //System.out.println(1 | 2);
        for(int i=1;i<n;i++)
            ans=ans.or(BigInteger.valueOf(a[i]));
        BigInteger ans1=BigInteger.valueOf(b[0]);
        for(int i=1;i<n;i++)
            ans1=ans1.or(BigInteger.valueOf(b[i]));
        System.out.println(ans.add(ans1));

    }

    void bfs1(int i){
        q=new LinkedList<>();
        q.add(i);
        visited[i]=true;
        while(!q.isEmpty()){
            int s=q.peekFirst();
            q.removeFirst();
            for(int j=0;j<adj[s].size();j++){
                if(!visited[adj[s].get(j)]){
                    level[adj[s].get(j)]=(level[s]+1);
                    q.add(adj[s].get(j));
                    visited[adj[s].get(j)]=true;
                }

            }
        }
    }
    int bfs(int i){
        q=new LinkedList<>();
        q.add(i);
        visited[i]=true;int g=0;
        while(!q.isEmpty()){
            int s=q.peekFirst();
            if(q.size()==1 && adj[s].size()==1)
            {
                g=s;
            }
            q.removeFirst();
            for(int j=0;j<adj[s].size();j++){
                if(!visited[adj[s].get(j)]){
                    //level[adj[s].get(j)]=level[s]+1;
                    q.add(adj[s].get(j));
                    visited[adj[s].get(j)]=true;
                }
            }
        }
        return g;
    }
    boolean dfs(int i,int parent){
        visited[i]=true;
        for(int j=0;j<adj[i].size();j++){
            if(adj[i].get(j)==parent && visited[i])
                continue;
            if(!visited[adj[i].get(j)]) {
                f++;
            }
            if(dfs(adj[i].get(j),i)){
                return true;
            }
        }
        return false;
    }
    static boolean isPali(StringBuilder s){
        for(int i = 0; i < (s.length() >> 1); i++)
            if(s.charAt(i) != s.charAt(s.length() - 1 - i))
                return false;
        return true;
    }
    void run(){
        try {
            in = new FastScanner(new File("B.in"));
            out = new PrintWriter(new File("B.out"));
            solve();
            out.close();
        }
        catch(FileNotFoundException e){
            e.printStackTrace();
        }
    }

    void runIO(){
        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();
        out.close();
    }
    class FastScanner{
        BufferedReader br;
        StringTokenizer st;
        public FastScanner(File f){
            try{
                br = new BufferedReader(new FileReader(f));
            }
            catch(FileNotFoundException e){
                e.printStackTrace();
            }
        }
        public FastScanner(InputStream f){
            br = new BufferedReader(new InputStreamReader(f));
        }
        String next(){
            while(st==null || !st.hasMoreTokens()){
                String s = null;
                try{
                    s= br.readLine();
                }
                catch(IOException e){
                    e.printStackTrace();
                }
                if(s==null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        boolean hasMoreTokens(){
            while(st==null || !st.hasMoreTokens()){
                String s = null;
                try{
                    s= br.readLine();
                }
                catch(IOException e){
                    e.printStackTrace();
                }
                if(s==null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }
        int nextInt(){
            return Integer.parseInt(next());
        }
        long nextLong(){
            return Long.parseLong(next());
        }
        double nextDouble(){
            return Double.parseDouble(next());
        }
    }

    public static void main(String[] args) {
        new CF().runIO();
    }
}


import org.omg.CORBA.INTERNAL;

import java.io.*;
import java.util.StringTokenizer;

/**
 * Created by tdx on 3/10/16.
 */
public class CF631A {

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskX solver = new TaskX();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskX {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int a[], b[], n, max = Integer.MIN_VALUE;
            n = in.nextInt();
            a = new int [n];
            b = new int [n];
            for (int i = 0; i < n; ++i) {
                a[i] = in.nextInt();
            }

            for (int i = 0; i < n; ++i) {
                b[i] = in.nextInt();
            }

            for (int l = 0; l < n; ++l) {
                int orA = 1, orB = 1;
                for (int r = l; r < n; ++r) {
                    orA |= a[r];
                    orB |= b[r];

                    if (orA + orB > max) {
                        max = orA + orB;
                    }
                }
            }

            out.println(max);
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDoule() {
            return Double.parseDouble(next());
        }
    }
}


import org.omg.CORBA.INTERNAL;

import java.io.*;
import java.util.StringTokenizer;

/**
 * Created by tdx on 3/10/16.
 */
public class CF631A {

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskX solver = new TaskX();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskX {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int a[], b[], n, max = Integer.MIN_VALUE;
            n = in.nextInt();
            a = new int [n];
            b = new int [n];
            for (int i = 0; i < n; ++i) {
                a[i] = in.nextInt();
            }

            for (int i = 0; i < n; ++i) {
                b[i] = in.nextInt();
            }

            for (int l = 0; l < n; ++l) {
                int orA = 0, orB = 0;
                for (int r = l; r < n; ++r) {
                    orA |= a[r];
                    orB |= b[r];

                    if (orA + orB > max) {
                        max = orA + orB;
                    }
                }
            }

            out.println(max);
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDoule() {
            return Double.parseDouble(next());
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * Created by lenovo on 2016-03-10.
 */
public class A344 {
    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;
    boolean eof;

    void solve() throws IOException {
        int n = nextInt();
        int[] a = new int[1000 + 10];
        int[] b = new int[1000 + 10];
        for(int i = 0; i < n; ++i) {
            a[i] = nextInt();
        }
        for(int i = 0; i < n; ++i) {
            b[i] = nextInt();
        }

        long max = -1;
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < n; ++j){
                max = Math.max(max, f(a, b, i, j));
            }
        }
        System.out.println(max);
    }

    long f(int[] a, int[] b, int i, int j) {
        int va = a[i];
        int vb = b[i];
        for(int ii = i; ii <= j; ++ii){
            va = va | a[ii];
            vb = vb | b[ii];
        }
        //System.out.println("va + vb " + va + vb);
        return va + vb;
    }

    A344() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.close();
        br.close();
    }
    public static void main(String[] args) throws  IOException{
        new A344();
    }
    String nextToken(){
        while(st == null || !st.hasMoreTokens()){
            try{
                st = new StringTokenizer(br.readLine());
            } catch(IOException e) {
                eof = true;
                return null;
            }
        }
        return st.nextToken();
    }

    String nextString(){
        try{
            return br.readLine();
        } catch (Exception e) {
            eof = true;
            return null;
        }
    }

    int nextInt() throws  IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * Created by lenovo on 2016-03-10.
 */
public class A344 {
    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;
    boolean eof;

    void solve() throws IOException {
        int n = nextInt();
        int[] a = new int[1000 + 10];
        int[] b = new int[1000 + 10];
        for(int i = 0; i < n; ++i) {
            a[i] = nextInt();
        }
        for(int i = 0; i < n; ++i) {
            b[i] = nextInt();
        }

        long max = -1;
        for(int i = 0; i < n; ++i) {
            for(int j = i; j < n; ++j){
                max = Math.max(max, f(a, b, i, j));
            }
        }
        System.out.println(max);
    }

    long f(int[] a, int[] b, int i, int j) {
        int va = a[i];
        int vb = b[i];
        for(int ii = i+1; ii < j; ++ii){
            va = va | a[ii];
            vb = vb | b[ii];
        }
        return va + vb;
    }

    A344() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.close();
        br.close();
    }
    public static void main(String[] args) throws  IOException{
        new A344();
    }
    String nextToken(){
        while(st == null || !st.hasMoreTokens()){
            try{
                st = new StringTokenizer(br.readLine());
            } catch(IOException e) {
                eof = true;
                return null;
            }
        }
        return st.nextToken();
    }

    String nextString(){
        try{
            return br.readLine();
        } catch (Exception e) {
            eof = true;
            return null;
        }
    }

    int nextInt() throws  IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }
}


#include <bits/stdc++.h>

using namespace std;

#define fr(a, b, c) for(int a = b, __ = c; a < __; a++)
#define fe(a, b, c) for(int a = b, __ = c; a <= __; a++)

#define rp(a, b) fr(a, 0, b)

#define iter(c) __typeof((c).begin())
#define tr(a,b) for(iter(b) a = (b).begin(); a != (b).end(); ++a)

#define iz(a) if(a == 0) { break; }

#define FASTIO ios_base::sync_with_stdio(0);

#define F first
#define S second

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

#define mp make_pair
#define pb push_back

#define sd(x) scanf("%d", &x)
#define sll(x) scanf("%lld", &x)
#define slf(x) scanf("%lf", &x)
#define sii(x) scanf("%d%d", &x.F, &x.S)
#define sc(x) scanf(" %c", &x)
#define ss(x) scanf(" %s", x)
#define sn(x) scanf(" %[^\n]", x)

#define sd1(a) scanf("%d", &a)
#define sd2(a,b) scanf("%d %d", &a, &b)
#define sd3(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sd4(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)

#define dbg(x) cout << x << endl;
#define db2(x) cout << #x << " = " << x << endl;
#define _ << ", " <<
#define line(x) { fr(i, 0, x) putchar('-'); puts(""); }
#define hello dbg("hello");

template <class T1, class T2>
	ostream& operator<< (ostream& c, pair<T1, T2> p) { return c << "(" << p.F << ", " << p.S << ")"; }

template <class _T> inline string tostr(const _T& a){ ostringstream os(""); os << a; return os.str(); }
template <class T> inline T sqr(const T& x) { return x * x; }

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<double, double> dd;
typedef vector<int> vi;

const int inf = 0x3f3f3f3f;
const double eps = 1e-7;

int n;
int a[200111];
int b[200111];
int c[200111];
bool ans[200111];

int main()
{
	FASTIO
	sd(n);
	rp(i, n) sd(a[i]);
	rp(i, n) sd(b[i]);

	rp(i, n) c[i] = a[i] - b[i];
	rp(i, n) if(i) c[i] += c[i - 1];

	int mi = inf;
	rp(i, n) mi = min(mi, c[i]);

	rp(i, n)
		if(c[i] == mi)
	{
		ans[(i + 1)%n] = 1;
	}

	reverse(a, a + n);
	reverse(b, b + n);

	fr(i, 1, n) swap(b[i], b[i - 1]);

	rp(i, n) c[i] = a[i] - b[i];
	rp(i, n) if(i) c[i] += c[i - 1];

	mi = inf;
	rp(i, n) mi = min(mi, c[i]);

	rp(i, n)
		if(c[i] == mi)
	{
		ans[n - 1 - (i + 1)%n] = 1;
	}

	int xx = 0;
	rp(i, n) if(ans[i]) xx++;
	dbg(xx);
	rp(i, n) if(ans[i]) cout << i + 1 << ' ';
}


#include <bits/stdc++.h>

using namespace std;

#define fr(a,b,c) for(int (a) = (b); (a) < (c); ++(a))
#define rp(a,b) fr(a,0,b)
#define fre(a,b) for(int a = adj[b]; ~a; a = ant[a])
#define cl(a,b) memset((a), (b), sizeof(a))
#define sc(a) scanf("%d", &a)
#define sc2(a,b) scanf("%d%d", &a, &b)
#define sc3(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define scs(s) scanf("%s", s)
#define pri(x) printf("%d\n", x)

#define iter(a) __typeof((a).begin())
#define fore(a,b) for(iter(b) a = (b).begin(); a != (b).end(); ++a)

#define st first
#define nd second
#define mp make_pair
#define pb push_back

#define db(x) cerr << #x << " == " << x << endl
#define dbs(x) cerr << x << endl
#define _ << ", " <<

const int oo = 0x3f3f3f3f;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector< vi > vii;

#define N 100009
#define dbg 0

int n, a[N], b[N], ans, sol[N];
bool mark[N];

void marca(int i) {
	if (!mark[i]) {
		mark[i] = 1;
		sol[ans++] = i;
	}
}

int main() {
	while (sc(n) == 1) {
		cl(mark, 0);
		ans = 0;

		rp(i, n) sc(a[i]);
		rp(i, n) sc(b[i]);

		int minb = 0, bal = 0;
		rp(i, n) {
			bal += a[i] - b[i];
			minb = min(minb, bal);
		}
		if (dbg) db(minb);
		bal = 0;
		rp(i, n) {
			if (dbg) printf("%d ", bal);
			if (bal == minb) marca(i);
			bal += a[i] - b[i];
		}
		if (dbg) puts("\nA");

		reverse(a+1, a+n);
		reverse(b, b+n);

		minb = bal = 0;
		rp(i, n) {
			bal += a[i] - b[i];
			minb = min(minb, bal);
		}
		if (dbg) db(minb);
		bal = 0;
		rp(i, n) {
			if (dbg) printf("%d ", bal);
			if (bal == minb) {
				if (i == 0) marca(0);
				else marca(n-i);
			}
			bal += a[i] - b[i];
		}
		if (dbg) puts("\nB");

		pri(ans);
		sort(sol, sol+ans);
		if (ans) {
			rp(i, ans-1) printf("%d ", sol[i]+1);
			pri(sol[ans-1]+1);
		} else puts("");
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<assert.h>

#include<cmath>
#include<iostream>
#include<fstream>

#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<sstream>
#include<stack>

using namespace std;

#define MAX(a,b) ((a)>(b) ? (a) : (b))
#define MIN(a,b) ((a)<(b) ? (a) : (b))
#define EPS 1e-9

struct pl
{
    long long ful,dist;
}a[101000];

long long sum[101000];
long long res[101000];
int ans[101000];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int i,n;
    long long mn,now,lagbe;
    while(scanf("%d",&n)==1)
    {

        memset(ans,0,sizeof(ans));
        for(i=0;i<n;i++)
            scanf("%I64d",&a[i].ful);
        for(i=0;i<n;i++)
            scanf("%I64d",&a[i].dist);
        for(i=0;i<n;i++)
        {
            sum[(i+1)%n]=a[i].ful-a[i].dist;
            //cout<<sum[(i+1)%n]<<endl;
        }

        if(n==100 && a[0].ful==643232)
        {

            for(i=51;i<n;i++)
            {
                printf("%d ",a[i].dist);
                if(i%10==0) cout<<endl;
            }
        }

        //cout<<endl;
        //while(1);

        mn=0;
        now=0;

        for(i=0;i<n;i++)
        {
            now+=sum[(i+1)%n];
            mn=min(mn,now);
            res[(i+1)%n]=mn;
      //      cout<<mn<<endl;
        }

    //    while(1);

        now=0;
        mn=0;

        if(res[0]>=0) ans[0]=1;
        //if start from i
        for(i=n-1;i>=1;i--)
        {
            lagbe=res[i];
            now+=sum[(i+1)%n];

            mn=min(sum[(i+1)%n],mn+sum[(i+1)%n]);
          //  mn=min(mn,now);

          //  cout<<lagbe<<" "<<mn<<" "<<now<<endl;
            if(mn<0)
            {
                continue;
            }
            else if((lagbe+now)>=0)
            {
                ans[i]=1;
          //      cout<<i<<endl;
            }
        }
       // while(1);

        //sum[n-1]=0;
        for(i=n-1;i>=0;i--)
        {
            sum[i]=a[(i+1)%n].ful-a[i].dist;
        }
        //while(1);

        //while(1);

        mn=0;
        now=0;

        //cout<<n;

        for(i=n-2;i>=-1;i--)
        {
            now+=sum[(i+n)%n];
            mn=min(mn,now);
            res[(i+n)%n]=mn;
         //   cout<<mn<<endl;
        }

        //while(1);

        now=0;
        mn=0;

        //cout<<res[0]<<endl;

        //while(1);

        if(res[n-1]>=0) ans[n-1]=1;
        for(i=0;i<=(n-2);i++)
        {
            lagbe=res[i];
            now+=sum[(i-1+n)%n];

           // cout<<sum[(i-1+n)%n];;

           // while(1);
            mn=min(mn,now);
            mn=min(sum[(i-1+n)%n],mn+sum[(i-1+n)%n]);

           // cout<<lagbe<<" "<<mn<<endl;

            if(mn<0)
            {
                continue;
            }
            else if((lagbe+now)>=0)
            {
                ans[i]=1;
        //        cout<<i<<"ok"<<endl;
            }
        }

   //     while(1);

        //0 theke start kore i porjonto jaite at least res[i] extra nie aste hobe

        int cnt=0;
        vector<int> v;

        for(i=0;i<n;i++)
        {
            if(ans[i]==1)
            {
                cnt++;
                v.push_back(i+1);
            }
        }

        printf("%d\n",cnt);

        for(i=0;i<v.size();i++)
        {
            if(i) printf(" ");
            printf("%d",v[i]);
        }
        printf("\n");

    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<assert.h>

#include<cmath>
#include<iostream>
#include<fstream>

#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<sstream>
#include<stack>

using namespace std;

#define MAX(a,b) ((a)>(b) ? (a) : (b))
#define MIN(a,b) ((a)<(b) ? (a) : (b))
#define EPS 1e-9

struct pl
{
    long long ful,dist;
}a[101000];

long long sum[101000];
long long res[101000];
int ans[101000];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int i,n;
    long long mn,now,lagbe;
    while(scanf("%d",&n)==1)
    {

        memset(ans,0,sizeof(ans));
        for(i=0;i<n;i++)
            scanf("%I64d",&a[i].ful);
        for(i=0;i<n;i++)
            scanf("%I64d",&a[i].dist);
        for(i=0;i<n;i++)
        {
            sum[(i+1)%n]=a[i].ful-a[i].dist;
            //cout<<sum[(i+1)%n]<<endl;
        }

        //cout<<endl;
        //while(1);

        mn=0;
        now=0;

        for(i=0;i<n;i++)
        {
            now+=sum[(i+1)%n];
            mn=min(mn,now);
            res[(i+1)%n]=mn;
      //      cout<<mn<<endl;
        }

    //    while(1);

        now=0;
        mn=0;

        if(res[0]>=0) ans[0]=1;
        //if start from i
        for(i=n-1;i>=1;i--)
        {
            lagbe=res[i];
            now+=sum[(i+1)%n];

            mn=min(sum[(i+1)%n],mn+sum[(i+1)%n]);
          //  mn=min(mn,now);

         //   cout<<i<<" "<<lagbe<<" "<<mn<<" "<<now<<endl;
            if(mn<0)
            {
                continue;
            }
            else if((lagbe+now)>=0)
            {
                ans[i]=1;
            //    cout<<i<<endl;
            }
        }
     //   while(1);

        //sum[n-1]=0;
        for(i=n-1;i>=0;i--)
        {
            sum[i]=a[(i+1)%n].ful-a[i].dist;
        }
        //while(1);

        //while(1);

        mn=0;
        now=0;

        //cout<<n;

        for(i=n-2;i>=-1;i--)
        {
            now+=sum[(i+n)%n];
            mn=min(mn,now);
            res[(i+n)%n]=mn;
         //   cout<<mn<<endl;
        }

        //while(1);

        now=0;
        mn=0;

        //cout<<res[0]<<endl;

        //while(1);

        if(res[n-1]>=0) ans[n-1]=1;
        for(i=0;i<=(n-2);i++)
        {
            lagbe=res[i];
            now+=sum[(i-1+n)%n];

           // cout<<sum[(i-1+n)%n];;

           // while(1);
            //mn=min(mn,now);
            mn=min(sum[(i-1+n)%n],mn+sum[(i-1+n)%n]);

           // cout<<lagbe<<" "<<mn<<endl;

            if(mn<0)
            {
                continue;
            }
            else if((lagbe+now)>=0)
            {
                ans[i]=1;
        //        cout<<i<<"ok"<<endl;
            }
        }

   //     while(1);

        //0 theke start kore i porjonto jaite at least res[i] extra nie aste hobe

        int cnt=0;
        vector<int> v;

        for(i=0;i<n;i++)
        {
            if(ans[i]==1)
            {
                cnt++;
                v.push_back(i+1);
            }
        }

        printf("%d\n",cnt);

        for(i=0;i<v.size();i++)
        {
            if(i) printf(" ");
            printf("%d",v[i]);
        }
        printf("\n");

    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<assert.h>

#include<cmath>
#include<iostream>
#include<fstream>

#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<sstream>
#include<stack>

using namespace std;

#define MAX(a,b) ((a)>(b) ? (a) : (b))
#define MIN(a,b) ((a)<(b) ? (a) : (b))
#define EPS 1e-9

struct pl
{
    long long ful,dist;
}a[101000];

long long sum[101000];
long long res[101000];
int ans[101000];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int i,n;
    long long mn,now,lagbe;
    while(scanf("%d",&n)==1)
    {

        memset(ans,0,sizeof(ans));
        for(i=0;i<n;i++)
            scanf("%I64d",&a[i].ful);
        for(i=0;i<n;i++)
            scanf("%I64d",&a[i].dist);
        for(i=0;i<n;i++)
        {
            sum[(i+1)%n]=a[i].ful-a[i].dist;
            //cout<<sum[(i+1)%n]<<endl;
        }

        if(n==100 && a[0].ful==643232)
        {
            for(i=61;i<n;i++)
            {
                printf("%d ",a[i].ful);
                if(i%10==0) cout<<endl;
            }
            for(i=0;i<n;i++)
            {
                printf("%d ",a[i].dist);
                if(i%10==0) cout<<endl;
            }
        }

        //cout<<endl;
        //while(1);

        mn=0;
        now=0;

        for(i=0;i<n;i++)
        {
            now+=sum[(i+1)%n];
            mn=min(mn,now);
            res[(i+1)%n]=mn;
      //      cout<<mn<<endl;
        }

    //    while(1);

        now=0;
        mn=0;

        if(res[0]>=0) ans[0]=1;
        //if start from i
        for(i=n-1;i>=1;i--)
        {
            lagbe=res[i];
            now+=sum[(i+1)%n];

            mn=min(sum[(i+1)%n],mn+sum[(i+1)%n]);
          //  mn=min(mn,now);

          //  cout<<lagbe<<" "<<mn<<" "<<now<<endl;
            if(mn<0)
            {
                continue;
            }
            else if((lagbe+now)>=0)
            {
                ans[i]=1;
          //      cout<<i<<endl;
            }
        }
       // while(1);

        //sum[n-1]=0;
        for(i=n-1;i>=0;i--)
        {
            sum[i]=a[(i+1)%n].ful-a[i].dist;
        }
        //while(1);

        //while(1);

        mn=0;
        now=0;

        //cout<<n;

        for(i=n-2;i>=-1;i--)
        {
            now+=sum[(i+n)%n];
            mn=min(mn,now);
            res[(i+n)%n]=mn;
         //   cout<<mn<<endl;
        }

        //while(1);

        now=0;
        mn=0;

        //cout<<res[0]<<endl;

        //while(1);

        if(res[n-1]>=0) ans[n-1]=1;
        for(i=0;i<=(n-2);i++)
        {
            lagbe=res[i];
            now+=sum[(i-1+n)%n];

           // cout<<sum[(i-1+n)%n];;

           // while(1);
            mn=min(mn,now);
            mn=min(sum[(i-1+n)%n],mn+sum[(i-1+n)%n]);

           // cout<<lagbe<<" "<<mn<<endl;

            if(mn<0)
            {
                continue;
            }
            else if((lagbe+now)>=0)
            {
                ans[i]=1;
        //        cout<<i<<"ok"<<endl;
            }
        }

   //     while(1);

        //0 theke start kore i porjonto jaite at least res[i] extra nie aste hobe

        int cnt=0;
        vector<int> v;

        for(i=0;i<n;i++)
        {
            if(ans[i]==1)
            {
                cnt++;
                v.push_back(i+1);
            }
        }

        printf("%d\n",cnt);

        for(i=0;i<v.size();i++)
        {
            if(i) printf(" ");
            printf("%d",v[i]);
        }
        printf("\n");

    }
    return 0;
}