#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pair<ll,ll>> vp;
#define pb(a)	push_back(a)
#define mp(a,b)	make_pair(a,b)
#define mod 1000000007
#define all(a)	a.begin(),a.end()
#define rall(a)	a.rbegin(),a.rend()
#define rep(i,a,b,c)	for(ll i=a;i<b;i+=c)
#define repn(i,a,b,c)	for(ll i=a;i>=b;i-=c)
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define yes cout<<"Yes\n"
#define no "no\n"
#define endl '\n'
#define F first
#define S second
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#define max(x,y)	((x<y)?y:x)
#define min(x,y)	((x<y)?x:y)
ll gcd(ll x, ll y){
	ll temp=min(x,y);
	y=x+y-temp;
	x=temp;
	while(y%x!=0){
	temp=x;
	x=y%x;
	y=temp;
	}
	return x;
}
bool prime[1000005];
vll spf(20000005,0);
void Seive(ll n){
	rep(i,0,n,1)	prime[i]=true;
	prime[0]=prime[1]=false;
    for (int p=2; p*p<n; p++) 
    { 
        if (prime[p]==false)	continue; 
        for (int i=p*p; i<n; i+=p) 
            prime[i] = false;  
    }
}
void Seive_spf(ll n){
	spf[0]=0, spf[1]=1;
    for (ll p=2; p<n; p++){
    	if (spf[p]>0)	continue;
    	spf[p]=p;
    	for(ll i=p;i<n;i+=p)	if(spf[i]==0)	spf[i]=p;
    }
}
ll nCrmodp_dp(ll n, ll r, ll p){
	ll c[r+1]={0};
	c[0]=1;
	rep(i,1,n+1,1){
		repn(j,min(i,r),0,1)	c[j] = (c[j]+c[j-1])%p;
	}
	return c[r];
}
ll nCrmodp(ll n, ll r, ll p){
	if(r==0)	return 1;
	return (nCrmodp(n/p, r/p, p)*nCrmodp_dp(n%p, r%p, p))%p;
}
void print2d(vvll a){
	rep(i,0,a.size(),1){
		rep(j,0,a[i].size(),1)	cout<<a[i][j]<<' ';
		cout<<endl;
	}
}
void print1d(vll a){
	rep(i,0,a.size(),1)	cout<<a[i]<<' ';
	cout<<endl;
}

void solve(){
	ll n;
	cin>>n;
	vll a(n), b(n);
	rep(i,0,n,1)	cin>>a[i];
	rep(i,0,n,1)	cin>>b[i];
	ll mi=a[0], ma=b[0];
	rep(i,1,n,1){
		mi=max(mi,a[i]);
		ma=min(ma,b[i]);
	}
	cout<<max(ma-mi+1,0)<<endl;
}

int main(){
	IOS
	#ifdef OK
	freopen("input.txt", "r", stdin); 
 	freopen("output.txt", "w", stdout);
 	#endif
	int t=1;
	//cin>>t;
	/*Seive(1000000)
	vll pr[1000000];
	rep(i,2,1000000,1)	if(prime[i]==false)	pr.pb(i);
	*/
	while(t--)	solve();
	return 0;
}
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
//#include<bits/stdc++.h>
#define int long long
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
#define mp make_pair
#define all(x) x.begin(),x.end()
#define puts(x) cout << (x) << "\n"
constexpr int mod = 998244353;
using namespace std;
int input() { int t; cin >> t; return t; }
int n, s, t, ndm, ndi, dt[234567], ans[234567];
vector<int>g[234567];
void f1(int i, int d, int p) {
	dt[i] = d;
	if (ndm < d) { ndm = d; ndi = i; }
	for (int j : g[i])if (j != p)f1(j, d + 1, i);
}
int f2t = 1;
void f2(int i, int p) {
	if (ans[i] == 0)ans[i] = f2t;
	for (int j : g[i])if (p != j && dt[j] > dt[i]) {
		f2t++; f2(j, i); f2t++;
	}
	for (int j : g[i])if (p != j && dt[j] < dt[i]) {
		f2t++; f2(j, i); f2t++;
	}
}
signed main() {
	//function<int(int, int)> gcd = [&gcd](int a, int b) {if (b == 0)return a; return gcd(b, a % b); };
	cin >> n;
	for0(i, n - 1) {
		int a, b; cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	ndm = 0; ndi = 1; f1(1, 0, 1); s = ndi;
	ndm = 0; ndi = 1; f1(s, 0, s); t = ndi;
	ndm = 0; ndi = 1; f1(t, 0, t);
	f2(s, s);
	for1(i, n) { cout << ans[i] << (i == n ? "\n" : " "); }
}