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

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int INF = int (1e9) + int (1e5);
const ll INFL = ll(2e18) + ll(1e10);
const ui MOD = 1E9 + 7;
const double EPS = 1e-9;
#define FOR(i,n) for (int i=0;i<(n);++i)
#define ROF(i,x) for(int i = (x) ; i >= 0 ; --i)
#define MP make_pair
#define all(a) (a).begin(),(a).end()
#define ODD(x) ( ((x)&1)==0?0:1 )
#define SIGN(x) ( ((x) > 0) - ((x) < 0)  )
#define dbg(x) cerr << #x"= " << x << endl

std::mt19937_64  generator(std::chrono::system_clock::now().time_since_epoch().count());

inline ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a, ll b) { return a ? gcd(b%a, a): b; }
ll lcm(ll a, ll b) { return a / gcd(a,b) * b; }

void READ(bool _local){
    ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef _DEBUG
    if (_local)
        freopen ("in.txt", "r", stdin);
#endif
}
const int N=1E5+1;
int c[N];
vi g[N];
int cnt[N];
int n;
int good[N];
void dfs(int node, int p=-1){
    if (++cnt[c[node]]==1) good[node+1]=1;
    for(int v:g[node]){
        if (v!=p){
            dfs(v,node);
        }
    }
    --cnt[c[node]];
}
int main() {
    READ(0);
    cin>>n;
    FOR(i,n) cin>>c[i];
    FOR(i,n-1){
        int u,v;cin>>u>>v;
        --u, --v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(0);
    for(int i=1;i<=n;++i) if (good[i]) cout << i << " ";
    return 0;
}

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

#define mp          make_pair
#define pb          push_back
#define pf          push_front
#define ff          first
#define ss          second
#define all(x)      x.begin(),x.end()
#define rall(x)     x.rbegin(),x.rend()
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b))/gcd((a),(b))
#define endl        "\n"
#define pi          3.14159265358979323846
#define inf         1000000000
#define setbits(x)  __builtin_popcountll(x)
#define ordered_set    tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

typedef long long ll;
typedef long double ld;

#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

const int N = 1e5+50;
const ll mod = 1e9+7;
const ll INF = 1e18;

// int dx[9]={-1,-1,-1,0,1,1,1,0,0};
// int dy[9]={-1,0,1,1,1,0,-1,-1,0};
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};

bool compare(const pair<ll, ll>&i, const pair<ll, ll>&j)
{
    return i.second < j.second;
}

ll fact[N];
vector<bool> is_prime(100000,true);
vector<int>prime;

void pre()
{
    fact[0]=1;
    for(ll i=1;i<N;i++)
    {
        fact[i]=i*fact[i-1];
        fact[i]%=mod;
    }
}

ll modexp(ll x,ll n)
{
    ll r=1;
    while(n){
        if(n&1)
            r=(r*x)%mod;

        x=(x*x)%mod;
        n>>=1;
    }
    return r%mod;
}

ll modinv(ll x)
{
    return modexp(x,mod-2);
}

ll ncr(ll n,ll r)
{
    if(n<r)
        return 0;

    ll ans=(fact[n]%mod * modinv(fact[r])%mod * modinv(fact[n-r])%mod)%mod;
    return ans;
}

ll exp(ll x,ll n)
{
    ll r=1;
    while(n){
        if(n&1)
            r=(r*x);

        x=(x*x);
        n>>=1;
    }
    return r;
}

int n;
vector<int>C(N);
vector<int>adj[N];
bool vis[N];
map<int,int>m;
int ver;

vector<int>ans;

void dfs(int x){
    vis[x]=1;
    if(m[C[x]]==0)
        ans.pb(x);
    m[C[x]]++;
    for(auto ch:adj[x]){
        if(!vis[ch]){
            dfs(ch);
        }
    }
    m[C[x]]--;
}

void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>C[i];
    for(int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    dfs(1);
    sort(ans.begin(),ans.end());
    for(auto x:ans)
        cout<<x<<endl;
    return ;
}

int32_t main()
{
    fio

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

    // pre();
    int t=1;
    // cin>>t;
    for(int i=0;i<t;i++)
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

void output(vector<int> &a){
  cout << a.size();
  for(auto &nx : a){
    cout << ' ' << nx;
  }
  cout << '\n';
}

int main(){
  int n;
  cin >> n;
  vector<int> a(n);
  for(auto &nx : a){cin >> nx;}
  vector<vector<int>> bk(200,vector<int>(0));

  int cnt=min(n,8);
  for(int i=1;i<(1<<cnt);i++){
    int sig=0;
    vector<int> s;
    for(int j=0;j<cnt;j++){
      if(i&(1<<j)){
        s.push_back(j+1);
        sig+=a[j];sig%=200;
      }
    }
    if(bk[sig].size()!=0){
      cout << "Yes\n";
      output(bk[sig]);
      output(s);
      return 0;
    }
    else{bk[sig]=s;}
  }
  cout << "No\n";
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define eni(x) template < class c > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
template < class c > struct rge { c b, e; };
template < class c > rge<c> range(c i, c j) { return rge<c>{i, j}; }
template < class c > auto dud(c* x) -> decltype(cerr << *x, 0);
template < class c > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; return * this; }
eni(==) return * this << range(begin(i), end(i)); }
template < class c, class b > debug & operator <<(pair < b, c > d) {
  return * this << "(" << d.first << ", " << d.second << ")";
}
template < class c > debug & operator <<(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  return * this << "]";
}
#else
template < class c > debug & operator <<(const c&) { return * this; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define mod 998244353
#define mx 1000050
mt19937 myrand(time(0));

ll fact[52];
int n,k,cnt;
int a[52][52];
vector<int> adj[52];
bool vis[52];

void dfs(int u){
    if(vis[u]) return;
    cnt++;
    vis[u]=1;
    for(auto& v:adj[u]){
        if(!vis[v]) dfs(v);
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(),cout.tie();
    fact[0]=1;
    for(int i=1;i<=50;i++) fact[i]=(fact[i-1]*i)%mod;
    cin>>n>>k;
    for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>a[i][j];
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            bool paisi=0;
            for(int l=0;l<n;l++){
                if(a[l][i]+a[l][j]>k){
                    paisi=1;
                    break;
                }
            }
            if(!paisi){
                adj[i].pb(j);
                adj[j].pb(i);
            }
        }
    }
    ll ans=1;
    for(int i=0;i<n;i++){
        if(!vis[i]){
            cnt=0;
            dfs(i);
            ans=(ans*fact[cnt])%mod;
        }
    }
    for(int i=0;i<n;i++) vis[i]=0,adj[i].clear();
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            bool paisi=0;
            for(int l=0;l<n;l++){
                if(a[i][l]+a[j][l]>k){
                    paisi=1;
                    break;
                }
            }
            if(!paisi){
                adj[i].pb(j);
                adj[j].pb(i);
            }
        }
    }
    for(int i=0;i<n;i++){
        if(!vis[i]){
            cnt=0;
            dfs(i);
            ans=(ans*fact[cnt])%mod;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

int main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (auto &&i : v) scanf("%d", &i);
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        int l = i, r = i;
        while(l > 0 && v[l-1] >= v[i]) l--;
        while(r < n && v[r] >= v[i]) r++;
        ans = max(ans, (r-l)*v[i]);
    }
    cout << ans << "\n";
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define INF 1000000007
#define INF_ll 1000000000000000007
#define ll long long
#define endl "\n"
#define uint unsigned long long
#define unmp unordered_map
#define unst unordered_set
#define int long long
#define watch(x) cerr << "\n" << (#x) << " is " << (x) << endl
#define line cout << " " << endl

/////////////////////////////////////////////////////
void hi(){
    static int i=0;
    cout << "Check Point : " << ++i << endl;
    return;
}
/////////////////////////////////////////////////////

/////////////////////////////////////////////////////
int stoi1(string str){
    int n=str.length();
    int number = 0;
    int pos = 1;
    for(int i=n-1;i>=0;i--){
        number = number + (str[i]-'0')*pos;
        pos=pos*10;
    }
    return number;
}

int power(int a,int n){
    int res = 1;
    while(n>0){
        if(n%2!=0){
            res = res*a;
            n--;
        }
        else if(n%2==0){
            a = a*a;
            n=n/2;
        }
    }
    return res;
}
/////////////////////////////////////////////////////

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //#ifndef ONLINE_JUDGE
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //#endif

    int t;
    t=1;
    //cin >> t;
    while(t--){
        int k;
        cin >> k;
        int pdt;
        int count=0;
        for(int i=1;i<=k;i++){
            for(int j=1;j<=k;j++){
                if(i*j>k) break;
                for(int l=1;l<=k;l++){
                    pdt = i*j*l;
                    if(pdt<=k) count++;
                    else break;
                }
            }
        }
        cout << count << endl;

    }
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<vector<vector<long long>>> vvvl;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef long long ll;
const long long MOD=1000000007;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound

int main(){

    int n;
    cin>>n;
    vl a(n);
    rep(i,n) cin>>a[i];

    ll sum=0;

    rep(i,n) sum+=a[i]*a[i];

    ll tmp=0;

    rep(i,n) tmp+=a[i];

    tmp=tmp*tmp;

    cout<<n*sum-tmp<<endl;

}

#include<stdio.h>
#include<queue>
#include<vector>
#include<string.h>
#include<iostream>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
#define int long long
#define ll long long
#define speed(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
const int maxn=2e5+10;
const int inf=0x3f3f3f3f;
const int INF=1ll<<62;

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

int gcd(int a,int b) {
	return b? gcd(b,a%b):a;
}

int n;

signed main() {
    n=rd();
	int ans=0;
    for(int i=1;i<=1000000;i++){
    	int ceng=1;
    	while(pow(10,ceng)<=i)ceng++;
    	int res=pow(10,ceng)*i+i;
//    	cout<<res<<endl;
    	if(res>n)break;
    	ans=i;
	}
	printf("%lld\n",ans);
	return 0;
}

//5 3 12


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

ll fact(ll n)
{
    ll f=1;
    for(int i=1;i<=n;i++){
        f*=i;
    }
    return f;
}

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

    string s;
    cin>>s;
    int n=s.size();
    int flag=0,no=0;
    int l=0,r=n-1;
    while(l<r){
        if(s[l]!=s[r] && flag==0){
            if(s[r]=='0'){
                r--;
                continue;
            }
            else{
                no=1;
                break;
            }
        }
        else if(s[l]==s[r]){
            flag=1;
            l++;r--;
        }
        else if(s[l]!=s[r]){
            no=1;
            break;
        }
    }
    if(no==1) cout<<"No";
    else cout<<"Yes";
}

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
typedef unsigned long long ull;
typedef long long ll;
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define nep(i,r,l) for(int i=r;i>=l;i--)
void sc(int &x){scanf("%d",&x);}
void sc(int &x,int &y){scanf("%d%d",&x,&y);}
void sc(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void sc(ll &x){scanf("%lld",&x);}
void sc(ll &x,ll &y){scanf("%lld%lld",&x,&y);}
void sc(ll &x,ll &y,ll &z){scanf("%lld%lld%lld",&x,&y,&z);}
void sc(char *x){scanf("%s",x);}
void sc(char *x,char *y){scanf("%s%s",x,y);}
void sc(char *x,char *y,char *z){scanf("%s%s%s",x,y,z);}
void out(int x){printf("%d\n",x);}
void out(ll x){printf("%lld\n",x);}
void out(int x,int y){printf("%d %d\n",x,y);}
void out(ll x,ll y){printf("%lld %lld\n",x,y);}
void out(int x,int y,int z){printf("%d %d %d\n",x,y,z);}
void out(ll x,ll y,ll z){printf("%lld %lld %lld\n",x,y,z);}
using namespace std;
const int N=5e3+5;
bool judge(int x)
{
    if(x==0) return true;
    vector<int>ans;
    while(x) ans.push_back(x%10),x/=10;
    for(int l=0,r=ans.size()-1;l<=r;l++,r--)
        if(ans[l]!=ans[r]) return false;
    return true;
}
int main()
{
    //freopen("1.in","r",stdin);freopen("1.out","w",stdout);
    int n;cin>>n;
    while(n&&n%10==0) n/=10;
    if(judge(n)) printf("Yes\n");
    else printf("No\n");
}


#include<bits/stdc++.h>
using namespace std;
namespace Sakurajima_Mai{
    #define ms(a) memset(a,0,sizeof(a))
    #define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
    #define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
    #define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
    #define ce(i,r) i==r?'\n':' '
    #define pb push_back
    #define all(x) x.begin(),x.end()
    #define gmn(a,b) a=min(a,b)
    #define gmx(a,b) a=max(a,b)
    #define fi first
    #define se second
    typedef long long ll;
    typedef unsigned long long ull;
    typedef double db;
    const int infi=2e9;//infi较大，注意涉及inf相加时爆int
    const ll infl=4e18;
    inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
    inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
    //std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Sakurajima_Mai;
namespace Fast_Read{
    inline int qi(){
        int f=0,fu=1; char c=getchar();
        while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
        while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
        return f*fu;
    }
    inline ll ql(){
        ll f=0;int fu=1; char c=getchar();
        while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
        while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
        return f*fu;
    }
    inline db qd(){
        char c=getchar();int flag=1;double ans=0;
        while((!(c>='0'&&c<='9'))&&c!='-') c=getchar();
        if(c=='-') flag=-1,c=getchar();
        while(c>='0'&&c<='9') ans=ans*10+(c^48),c=getchar();
        if(c=='.'){c=getchar();for(int Bit=10;c>='0'&&c<='9';Bit=(Bit<<3)+(Bit<<1)) ans+=(double)(c^48)*1.0/Bit,c=getchar();}
        return ans*flag;
    }
}
namespace Read{
    #define si(a) scanf("%d",&a)
    #define sl(a) scanf("%lld",&a)
    #define sd(a) scanf("%lf",&a)
    #define ss(a) scanf("%s",a)
    #define rai(x,a,b) repi(i,a,b) x[i]=qi()
    #define ral(x,a,b) repi(i,a,b) x[i]=ql()
}
namespace Out{
    #define pi(x) printf("%d",x)
    #define pl(x) printf("%lld",x)
    #define ps(x) printf("%s",x)
    #define pc(x) printf("%c",x)
    #define pe() puts("")
}
namespace DeBug{
    #define MARK false
    #define DB if(MARK)
    #define pr(x) cout<<#x<<": "<<x<<endl
    #define pra(x,a,b) cout<<#x<<": "<<endl; \
            repi(i,a,b) cout<<x[i]<<" ";     \
            puts("");
    #define FR(a) freopen(a,"r",stdin)
    #define FO(a) freopen(a,"w",stdout)
}
using namespace Fast_Read;
using namespace Read;
using namespace Out;
using namespace DeBug;
int qpow(int x,int n,int mod){ int res=1;while(n){ if(n&1) res=(1ll*res*x)%mod; x=1ll*x*x%mod,n>>=1; } return res; }
int main()
{
    int a=qi(),b=qi(),c=qi();
    int fac=qpow(b,c,4)+4;
    pi(qpow(a,fac,10)),pe();
    return 0;
}

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1000000007
using namespace std;

long power(long x, long y, long mod){
  if(y==0)return 1;
  else if(y==1)return x%mod;
  else if(y%2==0)return (power(x,y/2,mod)*power(x,y/2,mod))%mod;
  else return (((power(x,y/2,mod)*power(x,y/2,mod))%mod) *x)%mod;
}

int main(){
  long a,b,c;
  cin >> a >> b >> c;
  long f = a%10, count = 1, aa = (a%10)*(a%10);
  while(aa%10 != f){
    aa *= (a%10);
    count++;
  }
  // int bc = power(b,c,count);
  // if(bc == 0)bc += count;
  if(count == 1)cout << f << endl;
  else cout << power(a,power(b,c,count)+count,10) << endl;
  return 0;
}


#include <bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < (int)(Y);++(X))
#define reps(X,S,Y) for (int (X) = (int)(S);(X) < (int)(Y);++(X))
#define rrep(X,Y) for (int (X) = (int)(Y)-1;(X) >=0;--(X))
#define rreps(X,S,Y) for (int (X) = (int)(Y)-1;(X) >= (int)(S);--(X))
#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))
#define peat(X,Y) for (;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define Endl endl
#define NL <<"\n"
#define cauto const auto

using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
template<class T> using vv=vector<vector<T>>;
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
//#undef NUIP
#ifdef NUIP
#include "benri.h"
#else
#define out(args...)
#endif
void ouT(ll x,int d=3){auto re=to_string(x);if((int)re.size()>d) re=x>0?"oo":"-oo";cout<<string(d-re.size(),' ')<<re<<",";}
#ifdef __cpp_init_captures
template<typename T>vector<T> table(int n, T v){ return vector<T>(n, v);}
template <class... Args> auto table(int n, Args... args){auto val = table(args...); return vector<decltype(val)>(n, move(val));}
#endif
template<class A,class B> pair<A,B> operator+(const pair<A,B> &p,const pair<A,B> &q){ return {p.X+q.X,p.Y+q.Y};}
template<class A,class B,class C,class D> pair<A,B>& operator+=(pair<A,B> &p,const pair<C,D> &q){ p.X+=q.X; p.Y+=q.Y; return p;}
template<class A,class B> pair<A,B> operator-(const pair<A,B> &p,const pair<A,B> &q){ return {p.X-q.X,p.Y-q.Y};}
template<class A,class B,class C,class D> pair<A,B>& operator-=(pair<A,B> &p,const pair<C,D> &q){ p.X-=q.X; p.Y-=q.Y; return p;}
template<class A,class B> istream& operator>>(istream &is, pair<A,B> &p){ is>>p.X>>p.Y; return is;}
template<class T=ll> T read(){ T re; cin>>re; return move(re);}
template<class T=ll> T read(const T &dec){ T re; cin>>re; return re-dec;}
template<class T=ll> vector<T> readV(const int sz){ vector<T> re(sz); for(auto &x:re) x=read<T>(); return move(re);}
template<class T=ll> vector<T> readV(const int sz, const T &dec){ vector<T> re(sz); for(auto &x:re) x=read<T>(dec); return move(re);}
vv<int> readG(const int &n,const int &m){ vv<int> g(n); rep(_,m){ cauto a=read<int>(1),b=read<int>(1); g[a].pb(b); g[b].pb(a);} return move(g);}
vv<int> readG(const int &n){ return readG(n,n-1);}
#define TT cauto TTT=read();rep(_,TTT)
const ll MOD=1e9+7; //998244353;

int main(){
  ios_base::sync_with_stdio(false); cin.tie(0);
  cout<<fixed<<setprecision(0);
	cauto n=read();
	auto as=readV(n); reverse(all(as));
	cauto bs=readV(n);
	priority_queue<int,vector<int>,greater<int>> pq;
	ll re=0;
	rep(i,n){
		cauto a=max(as[i],bs[i]);
		cauto b=min(as[i],bs[i]);
		re+=a; pq.emplace(a);
		if(pq.top()<b){
			re-=pq.top();	pq.pop();
			re+=b; pq.emplace(b);
		}
	}
	cout<<re NL;
  return 0;
}

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define X first
#define Y second
#define nl '\n'
#define AC return 0
#define pb(a) push_back(a)
#define mst(a,b) memset(a, b, sizeof a)
#define rep(i,n) for(int i = 0; (i)<(n); i++)
#define rep1(i,n) for(int i = 1; (i)<=(n); i++)
#define scd(a) scanf("%lld", &a)
#define scdd(a,b) scanf("%lld%lld", &a, &b)
#define scs(s) scanf("%s", s)
//#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const ll INF = (ll)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18;
const int N = 1.5e6+10, M = 2e6+10, mod = 1e9+7, inf = 0x3f3f3f;
ll a[N], nex[N], pre[N];
map<int, int> mp;
vector<int> v[N];

int main()
{
	IOS;
	int _;
	_ = 1;
	while(_--)
	{
		ll n, m;
		cin>>n>>m;
		rep1(i, n)	cin>>a[i], v[a[i]].pb(i);
		rep1(i, n)
		{
			if(mp[a[i]])
				nex[mp[a[i]]] = i, pre[i] = mp[a[i]];
			mp[a[i]] = i;
		}
		rep1(i, n)
		{
			if(!nex[i])
				nex[i] = n+1;
			if(!pre[i])
				pre[i] = 0;
		}
		rep(i, n+1)
		{
			if(!v[i].size())
			{
				cout<<i<<nl;
				AC;
			}
			for(auto x : v[i])
				if(nex[x]-x > m||x-pre[x] > m)
				{
					cout<<i<<nl;
					AC;
				}
		}
	}

	AC;
}


/*
			Saturday 01 May 2021 05:47:35 PM IST
					@uthor::astrainL3gi0N
*/

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

typedef long long int ll;
typedef std::vector<int> vi;
typedef std::vector<ll> vll;
typedef std::pair<int,int> ii;
#define debg(x)	std::cerr<<(#x)<<" => "<<x<<'\n';
#define debgg(x,y)	std::cerr<<(#x)<<" => "<<x<<'\t'<<(#y)<<' '<<y<<'\n';
#define len(a) (int)(a).size()
#define all(x) x.begin(),x.end()
const int mod = 1000000007;
//const int mod = 998244353;

bool comp (int x, int y) {
    return x > y;
}
void printarr (int arr[], int n) {
    for(int i = 0; i < n; ++i)
        std::cerr<<arr[i]<<(i<n-1?' ':'\n');
}
template < typename T> void  printv (T &a) {
    for (auto it = a.begin(); it != a.end(); ++it)
        std::cerr<<*it<<' ';
    std::cerr<<'\n';
}
int gint() {
    int n;  cin>>n;
    return n;
}

const int maxn = 2009;
double ans;
double dt[maxn];
double ht[maxn];

void solve ()
{
    ans = 0;
    bool ok = 1;
    int n = gint();
    double d = gint();
    double h = gint();

    for (int i = 0; i < n;++i) {
        dt[i] = gint();
        ht[i] = gint();
    }
    n++;
    double slope=0 ,c=0;
    dt[n] = 0;

    for (int i = 0;ok &&  i < n;++i) {
        slope = (h-ht[i])/(d-dt[i]);
        c = h-slope*d;
        ok = 0;

        for (int j = 0; !ok && j < n;++j) {
            double temp = slope*dt[j]+c;

            ok = ht[j] > temp;

        }

    }
    ans = c;

    cout << setprecision(10) << fixed;
    std::cout<<ans;
    std::cout<<'\n';
}
void sol ()
{
    //test
}

int main ()
{
    //sol();return 0;
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);

    int testcases = 1;
    //testcases = gint();
    for (int t = 0; t < testcases; ++t) {
        //cout<<"Case #"<<t+1<<": ";
        solve();
    }

    return 0;
}

#include <iostream>
using namespace std;

int main(void){
  int N, D, H;
  cin >> N>> D>>H;
  double b, d, h;
  double ans = 0.0;
  for( int i=0; i<N; ++i){
    cin >> d >>h;
    b = ((double)D*h - (double)H*d) / ((double)D-d);
    if(ans < b) ans = b;
  }
  cout<<ans;
  return 0;
}


#include <iostream>
//#include <vector>
//#include <string>
//#include <algorithm>
//#include <math.h>
//#include <queue>
//#include <stack>
//#include <iomanip>

// sometimes used
//#include <set>
//#include <map>
//#include <numeric>
//#include <list>
//#include <deque>
//#include <unordered_map>

typedef long long LL;
//typedef long double LD;
using namespace std;

//#define MOD 1000000007
//#define MOD 998244353
//#define MAX 200100
//#define NIL -1
//#define INF 1000000000000000000
//const LL SEG_VAL = 1 << 19;

pair<LL, LL> exact(const LL &B, const LL &C){
    const auto n = C/2;
    if(C%2){
        return {-B-n, -B+n};
    }else{
        if(!C){
            return {B, B};
        }
        return {B-n, B+n-1};
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);

    LL B;
    LL C;
    cin >> B >> C;
    pair<LL, LL> ab = exact(B, C);
    pair<LL, LL> cd = exact(B, C-1);
    cout << (ab.second-ab.first+1)+(cd.second-cd.first+1)-max((LL)0, min(ab.second,cd.second)-max(ab.first,cd.first)+1) << endl;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define  ll long long
#define ld long double
#define f first
#define s second

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    ll b , c , l , r;
    vector< pair<ll,ll> > v;

    cin >> b >> c;

    //awel segment, min positive number a2dar ageboh
    r = b , l = b-c/2;
    v.push_back({l,r});

    //tany segment, min negative a2dar ageboh
    r = -b , l = -(b - (c-1)/2);
    v.push_back({l,r});

    //talet seg, max neg
    r = -b , l = -b - (c-1)/2;
    v.push_back({l,r});

    //rabe3 seg, max positive a2dar ageboh
    l = b , r = b + (c-2)/2;
    v.push_back({l,r});

    set<ll> st;
    for(int i=0; i<v.size(); i++)
    {
        if (v[i].f > v[i].s) swap(v[i].f , v[i].s);
    }
    sort(v.begin() , v.end());

    ll ans = 0;
    l = v[0].f , r = v[0].s;
    for(int i=1; i<v.size(); i++)
    {
        if (v[i].f <= r) r = max(r , v[i].s);
        else
        {
            ans += (r-l+1);
            l = v[i].f , r = v[i].s;
        }
    }

    ans += (r-l+1);
    cout << ans << '\n';

    return 0;
}


#include <bits/stdc++.h>
const int N = 300;
using std::cout;
bool f[N];
int n, id[N];
std::pair<int, int> p[N];
int main() {
 std::ios::sync_with_stdio(false);
 std::cin >> n;
 for (int i = 1; i <= n; ++i) {
  std::cin >> p[i].first >> p[i].second;
  if (~p[i].first) {
   if (id[p[i].first]) {
    cout << "No";
    return 0;
   }
   id[p[i].first] = -i;
  }
  if (~p[i].second) {
   if (id[p[i].second]) {
    cout << "No";
    return 0;
   }
   id[p[i].second] = i;
  }
 }
 f[0] = 1;
 for (int i = 1; i <= n; ++i) {
  for (int j = 1; j <= i && !f[i]; ++j) {
   bool flag = 1;
   for (int k = 1; k <= j; ++k) {
    int x = id[2 * (i - j) + k], y = id[2 * (i - j) + j + k];
    if (x > 0 || y < 0 || (x < 0 && y > 0 && x != -y)) {
     flag = 0;
     break;
    }
   }
   if (flag)
    f[i] |= f[i - j];
  }
 }
 cout << (f[n] ? "Yes" : "No");
 return 0;
}

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#define int long long int
const int mod = 1e9 + 7;
using namespace std;

int ple;

int f(vector<int>arr,vector<int>brr)
{
     sort(arr.begin(),arr.end());
	 sort(brr.begin(),brr.end());
	 int n=arr.size(),nn=brr.size();
	 int ret=mod*mod;
	 for(int i=0;i<n;i++)
	 {
         int ind=lower_bound(brr.begin(),brr.end(),arr[i])-brr.begin();
		 int st=max(0LL,ind-2);
		 int en=min(nn-1,ind+2);
         for(int j=st;j<=en;j++)
		 {
			 int cur=abs(arr[i]-brr[j]);
			 if(cur<ret)
			 {
				 ret=cur;
				 ple=brr[j];
			 }
		 }
	 }
	 return ret;
}

int ff(vector<int>arr,vector<int>brr,vector<int>crr)
{
      int ret=f(arr,crr);
	  for(int &a:crr)
	  {
		  if(a==ple)
		  {
			  a=mod*mod;
			  break;
		  }
	  }
	  ret+=f(brr,crr);
	  return ret;
}

void myfun()
{
	int n;
	cin>>n;
	vector<int>rr,gg,bb;
	int r=0,b=0,g=0;
	for(int i=0;i<(2*n);i++)
	{
		int a; cin>>a;
		char ch; cin>>ch;
		if(ch=='R')
		{
			r++;
			rr.push_back(a);
		}
		if(ch=='G')
		{
			g++;
			gg.push_back(a);
		}
		if(ch=='B')
		{
			b++;
			bb.push_back(a);
		}
	}
	if(r%2==0 && g%2==0 && b%2==0)
	{
		cout<<"0\n";
		return;
	}
	int ans=mod*mod;
	if(r%2==1 && g%2==1)
        ans=min(ff(rr,gg,bb),f(rr,gg));
	if(r%2==1 && b%2==1)
		  ans=min(ff(rr,bb,gg),f(bb,rr));
	if(b%2==1 && g%2==1)
	  ans=min(ff(bb,gg,rr),f(gg,bb));
	cout<<ans<<"\n";
}

signed main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	int t=1;
	//cin>>t;
	while(t--)
	myfun();
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#define N 55
using namespace std;

typedef long long ll;
ll n, ans = 1e18, a[N];
bool vpr[N];
vector<ll> pr;

ll gcd(ll p, ll q) {
	if (q == 0) return p;
	return gcd(q, p % q);
}

void f(ll p, ll q) {
	ll i;
	if (q >= ans) return;
	if (p == pr.size()) {
		for (i = 0; i < n; i++) {
			if (gcd(q, a[i]) == 1) return;
		}
		ans = q;
		return;
	}
	f(p + 1, q);
	f(p + 1, q * pr[p]);
}

int main()
{
	ll i, j;
	cin >> n;
	for (i = 0; i < n; i++) {
		scanf("%lld", &a[i]);
	}
	for (i = 2; i <= 50; i++) {
		if (vpr[i]) continue;
		pr.push_back(i);
		for (j = i * 2; j <= 50; j += i) {
			vpr[j] = 1;
		}
	}
	f(0, 1);
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,srt,end) for(ll i = (srt); i < (ll)(end); i++)
ll MOD=1000000007;
ll mod=998244353;
int inf=1000001000;
ll INF=1e18+5;
//cout << std::fixed << std::setprecision(15) << y << endl;

// For debug
// Ref: https://qiita.com/ysuzuki19/items/d89057d65284ba1a16ac
#define dump(var)  do{std::cerr << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cerr << e << "\n";}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << "\n";}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cerr << "\n"; for(const auto& v : vv){ view(v); } }
template<typename T> void dump_cout(const T& v) { for(long long i = 0; i < v.size(); i++) std::cout << v[i] << (i == v.size()-1 ? "\n" : " "); }
// 約数列挙
vector<ll> yakusu(ll n){
    vector<ll> ret;
    for(ll i = 1; i*i <= n; i++){
        if(n % i != 0) continue;
        ret.push_back(i);
        if(i*i != n) ret.push_back(n/i);
    }
    sort(ret.begin(), ret.end());
    return ret;
}

bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2){
        if (num % i == 0){
            // 素数ではない
            return false;
        }
    }
    // 素数である
    return true;
}

// 最大公約数
// 結合法則も成り立つ
ll gcd(ll a, ll b){
    if(a < b) return gcd(b, a);
    ll r = a % b;
    while(r != 0){
        a = b;
        b = r;
        r = a % b;
    }
    return b;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n; cin >> n;
    vector<ll> x(n);
    rep(i,0,n) cin >> x[i];

    vector<ll> px;
    rep(i,2,51) if(IsPrime(i)) px.push_back(i);

    dump(px);
    ll ans = INF;
    for(int i = 0; i < (1<<px.size()); i++){
        ll y = 1;
        for(int j = 0; j < px.size(); j++) if(i & (1<<j)) y *= px[j];
        bool f = true;
        if(y != 1){
            rep(i,0,n){
                if(gcd(x[i], y) != 1) f *= true;
                else f *= false;
            }
        }else f = false;
        if(f) ans = min(ans, y);
    }
    cout << ans << endl;
    return 0;
}


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

#define ALL(obj) (obj).begin(),(obj).end()
template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>;

constexpr long long MOD = 1'000'000'000LL + 7; //'
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;}
int msb(int x) {return x?31-__builtin_clz(x):-1;}
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

/**
 * @url
 * @est
 */
int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    int N,M; cin >> N >> M;
    vector<int> A(N+1),B(M+1);
    for(int i=1;i<=N;++i) cin >> A[i];
    for(int i=1;i<=M;++i) cin >> B[i];
    auto dp = multivector(N+1,M+1,2,123456789);
    dp[0][0][0]=0;
    for(int i=0;i<=N;++i) {
        for(int j=0;j<=M;++j) {
            if(i+1<=N && j+1<=M) {
                int flg = (A[i+1]!=B[j+1]);
                chmin(dp[i+1][j+1][flg],dp[i][j][0]+flg);
                chmin(dp[i+1][j+1][flg],dp[i][j][1]+flg);
            }
            if(i+1<=N) {
                int flg = (A[i+1]!=B[j]);
                chmin(dp[i+1][j][flg],dp[i][j][1]+flg);
                chmin(dp[i+1][j][0]  ,dp[i][j][0]+1);
            }
            if(j+1<=M) {
                int flg = (A[i]!=B[j+1]);
                chmin(dp[i][j+1][flg],dp[i][j][1]+flg);
                chmin(dp[i][j+1][0]  ,dp[i][j][0]+1);
            }
        }
    }
    cout << min(dp[N][M][0],dp[N][M][1]) << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

int main(){
    int N, M;
    cin >> N >> M;
    vector<int> A(N), B(M);
    rep(i, N) cin >> A[i];
    rep(i, M) cin >> B[i];

    vector<vector<int>> dp(N+1, vector<int>(M+1, inf));
    rep(i, N+1) dp[i][0] = i;
    rep(i, M+1) dp[0][i] = i;

    rep2(i, 1, N){
        rep2(j, 1, M){
            chmin(dp[i][j], dp[i-1][j]+1);
            chmin(dp[i][j], dp[i][j-1]+1);
            int x = (A[i-1] == B[j-1]);
            chmin(dp[i][j], dp[i-1][j-1]+1-x);
        }
    }

    cout << dp[N][M] << '\n';
}


#include<bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<double,double> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const ll mod=1e9 + 7;
const ll LINF=1ll<<60;
const int INF=1<<28;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

typedef vector<vector<ll>> mat;

mat mul(mat A,mat B){
    mat C(A.size(),vector<ll> (B[0].size()));
    for(int i=0;i<A.size();i++){
        for(int k=0;k<B.size();k++){
            for(int j=0;j<B[0].size();j++){
                C[i][j]=(C[i][j]+A[i][k]*B[k][j])%mod;
            }
        }
    }
    return C;
}

mat pow(mat A,ll n){
    mat B(A.size(),vector<ll> (A.size()));
    for(int i=0;i<A.size();i++){
        B[i][i]=1;
    }
    while(n>0){
        if(n&1) B=mul(B,A);
        A=mul(A,A);
        n>>=1;
    }
    return B;
}

ll fpow(ll a, ll n){
    ll x = 1;
    if(a == 0) return 1;
    while(n > 0){
        if(n&1){
            x=x*a%mod;
        }
        a=a*a%mod;
        n >>= 1;
    }
    return x;
}

int main(){
    ll n,m,k;cin >> n >> m >> k;
    mat a(n, vector<ll> (1));
    for (int i = 0; i < n; i++) {
        cin >> a[i][0];
    }
    if(m == 0){
        for (int i = 0; i < n; i++) {
            cout << a[i][0] << endl;
        }
        return 0;
    }
    mat h(n, vector<ll> (n, 0));
    for (int i = 0; i < m; i++) {
        int x,y;cin >> x >> y;
        x--,y--;
        h[x][y]++;
        h[x][x]++;
        h[y][x]++;
        h[y][y]++;
        for (int j = 0; j < n; j++) {
            if(x == j || y == j) continue;
            h[j][j]+=2;
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            //cout << h[i][j] << " ";
            h[i][j] = h[i][j] * fpow(2, mod - 2) % mod * fpow(m, mod - 2) %mod;
        }
        //cout << endl;
    }
    auto res = mul(pow(h, k), a);
    for (int i = 0; i < n; i++) {
        cout << res[i][0] << endl;
        //cout << res[i][0] * fpow(fpow(2, k), mod - 2) %mod << endl;
    }
    return 0;
}

#include <bits/stdc++.h>

#define rep(i,n) for(LL i=0;i<n;++i)
#define repn(i,n) for(LL i=1;i<=n;++i)
#define LL long long
#define pii pair <LL,LL>
#define pb push_back
#define fi first
#define se second
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

LL qpow(LL x,LL a)
{
	LL res=x,ret=1;
	while(a>0)
	{
		if((a&1LL)==1LL) ret=ret*res%MOD;
		a>>=1LL;
		res=res*res%MOD;
	}
	return ret;
}

LL n,m,t,a[110];
vector <LL> g[110];
vector <vector <LL> > pw[40],res,tmpp,mulv;
vector <LL> tmp;

vector <vector <LL> > mul(vector <vector <LL> > a,vector <vector <LL> > b)
{
	vector <vector <LL> > ret(a.size(),vector <LL>(b[0].size()));
	rep(i,a.size()) rep(j,b[0].size()) rep(k,b.size()) ret[i][j]=(ret[i][j]+a[i][k]*b[k][j]%MOD)%MOD;
	return ret;
}

void getpow()
{
	repn(i,30) pw[i]=mul(pw[i-1],pw[i-1]);
}

int main()
{
  cin>>n>>m>>t;
  repn(i,n) scanf("%lld",&a[i]);
  LL x,y;
  rep(i,m)
  {
    scanf("%lld%lld",&x,&y);
    g[x].pb(y);g[y].pb(x);
  }
  if(t==0)
  {
    repn(i,n) cout<<a[i]<<endl;
    return 0;
  }
  pw[0].clear();
  LL add=1*qpow(m*2,MOD-2);
  repn(i,n)
  {
    tmp.clear();
    repn(j,n) tmp.pb(0);
    tmp[i-1]=(m-g[i].size())*qpow(m,MOD-2)%MOD;
    rep(j,g[i].size())
    {
      tmp[g[i][j]-1]=(tmp[g[i][j]-1]+add)%MOD;
      tmp[i-1]=(tmp[i-1]+add)%MOD;
    }
    pw[0].pb(tmp);
  }
  getpow();
  bool have=false;
  for(LL i=30;i>=0;--i)
  {
    if(t<(1LL<<i)) continue;
    t-=(1LL<<i);
    if(!have) mulv=pw[i];
    else mulv=mul(mulv,pw[i]);
    have=true;
  }
  repn(i,n)
  {
    tmp.clear();
    tmp.pb(a[i]);
    tmpp.pb(tmp);
  }
  res=mul(mulv,tmpp);
  rep(i,n) cout<<res[i][0]<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const long double k=0.6180339887498948482045;
ll x,y,tmp;
vector<int> ans;
int main(){
	scanf("%lld",&tmp);if(tmp==0) {printf("0\n");return 0;}
	//printf("%lf\n",((long double)tmp)*k-50);
	for(ll i=max((ll)(((long double)tmp)*k)-50,0ll);;i++){
		x=tmp;y=i;ans.clear();
		while(x&&y){
			if(x>y) ans.push_back(3),x=x-y;
			else ans.push_back(4),y=y-x;
			if(ans.size()>130) {break;}
		}
		if(ans.size()+max(x,y)<=130){
			if(x) for(int i=1;i<=x;i++) ans.push_back(1);
			else for(int i=1;i<=y;i++) ans.push_back(2);
			printf("%d\n",ans.size());
			for(int i=ans.size()-1;i>=0;i--) printf("%d\n",ans[i]);
			return 0;
		}
	}
	return 0;
}/*
47584759575744849
*/

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<climits>
#include<stack>
#include<queue>
#include<set>
#include<array>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cctype>
#include<cstring>
#include<ctime>
#include<functional>
#include<numeric>
#include<map>
#include<list>
#include<bitset>
#include<random>
#include<chrono>
using namespace std;

#define int long long

int fib[100], fibtop;

void makefib()
{
    fib[1] = 1;
    for (int i = 2; fib[i - 1] < LONG_LONG_MAX / 3; i++)
    {
        fib[i] = fib[i - 1] + fib[i - 2];
        fibtop = i + 1;
    }
}

vector<int> getfibfacs(int i)
{
    vector<int> r;
    while (i)
    {
        auto p = lower_bound(fib, fib + fibtop, i);
        if (*p != i) p--;
        r.push_back(p - fib);
        i -= *p;
    }
    return r;
}

signed main()
{
    makefib();
    int n;
    scanf("%lld", &n);
    vector<int> fibfacs = getfibfacs(n);
    vector<int> res;
    bool resultin;
    int curmin;
    if (fibfacs[0] % 2)
        resultin = true;
    else
        resultin = false;
    curmin = 1;
    res.push_back(1);
    for (int i = 1; i < fibfacs[0]; i++)
    {
        res.push_back(curmin ? 4 : 3);
        curmin = !curmin;
    }
    vector<int> inserts(res.size() + 5);
    for (int i = 1; i < (int)fibfacs.size(); i++)
    {
        inserts[fibfacs[0] - fibfacs[i]] = (fibfacs[0] - fibfacs[i]) % 2 ? 2 : 1;
    }
    auto f = [&](int r) -> int
    {
        if (resultin) return r;
        else
        {
            if (r == 1) return 2;
            if (r == 2) return 1;
            if (r == 3) return 4;
            return 3;
        }
    };
    vector<int> res2;
    for (int i = 0; i < (int)res.size(); i++)
    {
        res2.push_back(f(res[i]));
        if (inserts[i]) res2.push_back(f(inserts[i]));
    }
    printf("%lld\n", res2.size());
    for (int i = 0; i < (int)res2.size(); i++)
    {
        printf("%lld\n", res2[i]);
    }
    return 0;
}


#include<bits/stdc++.h>

#define ll long long int

using namespace std;

int seiv[200007];
ll res[200007];

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

    memset(seiv, 0, sizeof(seiv));
    memset(res, 0, sizeof(res));
    res[1] = 1;
    int k;
    cin >> k;
    if (k == 1) {
        cout << 1 << endl;
        return 0;
    }
    for (int i=2; i<=k; i++) {
        if (!seiv[i]) {
            res[i] = res[i-1] + 3;
            for (int j=2; j*i <= k; j++) {
                seiv[j*i] = 1;
                int cnt = 1, tmp = j;
                while (tmp%i == 0) {
                    cnt++;
                    tmp /= i;
                }
                if (!res[i*j]) res[i*j] = (cnt + 2)*(cnt + 1)/2;
                else res[i*j] *= (cnt + 2)*(cnt + 1)/2;
            }
        }
        else {
            res[i] += res[i-1];
        }
    }
    cout << res[k] << endl;

    return 0;
}

#include "bits/stdc++.h"

using namespace std;
using LL = long long;

constexpr int N = 1e5 + 5;

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);

	int n;
	cin >> n;

	LL res = 0;
	for (int i = 1; i * i * i <= n; i++) {
		for (int j = i; i * j * j <= n; j++) {
			int k = n / (i * j);
			if (i == j) {
				res += (k - j) * 3 + 1;
			}
			else {
				res += (k - j) * 6 + 3;
			}
		}
	}

	cout << res << endl;
}


//雪花飄飄北風嘯嘯
//天地一片蒼茫

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl

#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound

#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int n,nn;
int arr[1005],brr[1005];
map<int,int> m;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);

	cin>>n>>nn;
	rep(x,0,n) cin>>arr[x];
	rep(x,0,nn) cin>>brr[x];

	rep(x,0,n) m[arr[x]]++;
	rep(x,0,nn) m[brr[x]]++;

	for (auto &it:m){
		if (it.se==1) cout<<it.fi<<" ";
	}
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=1e5+2;

int num[maxn];

int f[maxn][2],last[maxn][2];

bool check(int x,int y)
{
	int v=num[x]^num[y],res=0;
	while(v) res++,v^=v&(-v);return res&1;
}

void solve()
{
	int N,L;scanf("%d%d",&N,&L);

	for(int i=1;i<=N;i++)
	{
		for(int j=L-1;j>=0;j--)
		{
			char x;scanf(" %c",&x);
			num[i]|=((x-'0')<<j);
		}
	}

	f[1][0]=f[1][1]=0;

	for(int i=2;i<=N;i++)
	{
		last[i][1]=( check(i,i-1)?i-1:last[i-1][1] );
		f[i][1]=f[last[i][1]][0];if(last[i][1]) f[i][1]++;

		last[i][0]=( last[i][1]==i-1?last[i-1][1]:i-1 );
		f[i][0]=f[last[i][0]][0];if(last[i][0]) f[i][0]++;

//		printf("%d %d %d %d\n",f[i][0],f[i][1],last[i][0],last[i][1]);
	}

	long long ans=0;

	for(int i=2;i<=N;i++) ans+=f[i][1];

	printf("%lld",ans);

	return;
}

int main()
{
	int kase=1;
	while(kase--) solve();
	return 0;
}


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

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int _t = 1;
	//cin >> _t;
	while(_t--)
{
	float x1,y1,x2,y2;
	cin >> x1 >> y1 >> x2 >> y2;
	float ans = x1 + ( (x2-x1)/(1+y2/y1) );
	cout << fixed << setprecision(8) << ans << endl;
}
	return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <algorithm>
#include <stack>
#include <queue>
#include <utility>
#include <cmath>
#include <set>
#include <iomanip>
using namespace std;
typedef long long ll;

int main()
{
	double sx, sy, gx, gy;
	cin >> sx >> sy >> gx >> gy;

	gy *= -1;
	double x_diff = gx - sx;
	double y_diff = gy - sy;

	double B = sy * x_diff - sx * y_diff;

	double ans = -1 * B / y_diff;

	std::cout << std::fixed <<std::setprecision(10) << ans << std::endl;
	return (0);
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ldb double
template<typename T>void ckmn(T&a,T b){a=min(a,b);}
template<typename T>void ckmx(T&a,T b){a=max(a,b);}
void rd(int&x){scanf("%i",&x);}
void rd(ll&x){scanf("%lld",&x);}
void rd(char*x){scanf("%s",x);}
void rd(ldb&x){scanf("%lf",&x);}
void rd(string&x){scanf("%s",&x);}
template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);}
template<typename T>void rd(vector<T>&x){for(T&i:x)rd(i);}
template<typename T,typename...A>void rd(T&x,A&...args){rd(x);rd(args...);}
template<typename T>void rd(){T x;rd(x);return x;}
int ri(){int x;rd(x);return x;}
template<typename T>vector<T> rv(int n){vector<T> x(n);rd(x);return x;}
template<typename T>void ra(T a[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]);}
template<typename T1,typename T2>void ra(T1 a[],T2 b[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]);}
template<typename T1,typename T2,typename T3>void ra(T1 a[],T2 b[],T3 c[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]),rd(c[st+i]);}
void re(vector<int> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){rd(u,v);E[u].pb(v);if(!dir)E[v].pb(u);}}
template<typename T>void re(vector<pair<int,T>> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){T w;rd(u,v,w);E[u].pb({v,w});if(!dir)E[v].pb({u,w});}}

const int mod=1e9+7;//998244353
int add(int a,int b){a+=b;return a>=mod?a-mod:a;}
void ckadd(int&a,int b){a=add(a,b);}
int sub(int a,int b){a-=b;return a<0?a+mod:a;}
void cksub(int&a,int b){a=sub(a,b);}
int mul(int a,int b){return (ll)a*b%mod;}
void ckmul(int&a,int b){a=mul(a,b);}
int powmod(int x,int k){int ans=1;for(;k;k>>=1,ckmul(x,x))if(k&1)ckmul(ans,x);return ans;}
int inv(int x){return powmod(x,mod-2);}

// D

int main(){
	int n,m;
	rd(n,m);
	auto a=rv<int>(n);
	int sumA=0;
	for(int i:a)sumA+=i;
	int N=m+n;
	int K=sumA+n;
	int ans=1;
	if(K>N)ans=0;
	else{
		for(int i=1;i<=K;i++){
			ckmul(ans,N-i+1);
			ckmul(ans,inv(i));
		}
	}
	printf("%i\n",ans);
	return 0;
}

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0() {}
template<typename H,typename... T> void print0(H h,T... t){cout<<fixed<<setprecision(15)<<h;print0(t...);}
void print() { print0("\n"); }
template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
void ioinit() { cout << fixed << setprecision(15); ios_base::sync_with_stdio(0); cin.tie(0); }
// clang-format on

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

vector<ll> mkprimes(ll N) {
    vector<bool> isprime(N + 1);
    vector<ll> primes;
    for (ll i = 0; i <= N; i++) {
        isprime[i] = true;
    }
    isprime[0] = false;
    isprime[1] = false;

    for (ll i = 2; i <= N; i++) {
        if (isprime[i]) {
            primes.push_back(i);
            for (ll j = 2 * i; j <= N; j += i) {  // 素数iの倍数は素数ではない
                isprime[j] = false;
            }
        }
    }
    return primes;
}

void update(ll org, vector<ll> &primes, ll ngp, set<ll> &result) {
    if (org > 10000) return;
    ll c = primes.size();
    result.insert(org);
    for (ll j = 0; j < c; j++) {
        if (j != ngp) {
            update(org * primes[j], primes, ngp, result);
        }
    }
}
int main() {
    vector<ll> primes = mkprimes(10000);
    ll c = primes.size();

    set<ll> r0;
    update(6, primes, 2, r0);
    update(10, primes, 1, r0);
    update(15, primes, 0, r0);
    update(30, primes, INF, r0);
    vector<ll> r3 = {6, 10, 15};
    for (auto p : r0) {
        if (p != 6 && p != 10 && p != 15) {
            r3.push_back(p);
        }
    }

    ll n;
    cin >> n;
    {
        //print(r3.size());
        if (r3.size() < n) return 0;
        vector<ll> r4;
        for (ll i = 0; i < n; i++) {
            r4.push_back(r3[i]);
        }
        for (ll i = 0; i < n; i++) {
            for (ll j = i + 1; j < n; j++) {
                assert(gcdf(r4[i], r4[j]) > 1);
                assert(r4[i] != r4[j]);
            }
        }
        ll g = r4[0];
        for (ll i = 0; i < n; i++) {
            g = gcdf(g, r4[i]);
        }
        assert(g == 1);
    }
    for (ll i = 0; i < n; i++) {
        if (i > 0) print0(" ");
        print0(r3[i]);
    }
    print();
}


#include <bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin >> n;
	vector<int> ans = {6, 10, 15};
	int x = 16;
	while (ans.size() < n) {
		if (x % 6 == 0 || x % 10 == 0 || x % 15 == 0) {
			ans.push_back(x);
		}
		x++;
	}
	for (int i = 0; i < n; i++) {
		cout << ans[i];
		if (i == n - 1)
			cout << endl;
		else
			cout << " ";
	}
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <iostream>
#include <fstream>

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

inline int inint(istream& in) {int x; in >> x; return x;}
inline ll inll(istream& in) {ll x; in >> x; return x;}
inline string instr(istream& in) {string x; in >> x; return x;}

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

class DAabAbaBaa {
public:
void solve(std::istream& in, std::ostream& out) {
    int A,B;
    ll K;
    A = inint(in);
    B = inint(in);
    K = inll(in);

    string ans = "";

    ll comb[62][62] = {0};
    comb[1][1] = 1;
    for (int i = 0; i < 61; ++i) {
        for (int j = 0; j < i  + 1; ++j) {
            comb[i + 1][j] += comb[i][j];
            comb[i + 1][j + 1] += comb[i][j];
        }
    }

    while(A + B > 0){
        ll now = comb[A + B][A];
        if(K > now){
            K -= now;
            B--;
            out << "b";
        }else{
            out << "a";
            A--;
        }
    }

    vector<int> re;

    out << endl;

}
};

int main() {
	DAabAbaBaa solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}

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

long long A, B, K;
vector<int> ans(100, 0);

long long COM(long long n, long long k) {
	long long ans = 1;
	for (long long i = 1; i <= k; i++) {
		ans = ans * (n - i + 1) / i;
	}
  	return ans;
}

void saiki(long long countA, long long now, long long rank) {
	if (now == A + B) {
		return;
	}
	else if (countA != A) {
		if ((K - rank) <= COM(A + B - now - 1, A - countA - 1)) {
			ans[now] = 1;
			saiki(countA + 1, now + 1, rank);
		}
		else {
			ans[now] = 2;
			saiki(countA, now + 1, rank + COM(A + B - now - 1, A - countA - 1));
		}
	}
	else {
		ans[now] = 2;
		saiki(countA, now + 1, rank + COM(A + B - now - 1, A - countA - 1));
	}
}

int main() {
	cin >> A >> B >> K;
	string S;
  	saiki(0, 0, 0);
	for (int i = 0; i < A + B; i++) {
		if (ans[i] == 1) S = S + 'a';
		else S = S + 'b';
	}
	cout << S << endl;
}


/* Clearink */

#include <map>
#include <cstdio>

inline int rint () {
	int x = 0, f = 1; char s = getchar ();
	for ( ; s < '0' || '9' < s; s = getchar () ) f = s == '-' ? -f : f;
	for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
	return x * f;
}

template<typename Tp>
inline void wint ( Tp x ) {
	if ( x < 0 ) putchar ( '-' ), x = ~ x + 1;
	if ( 9 < x ) wint ( x / 10 );
	putchar ( x % 10 ^ '0' );
}

std::map<int, int> buc;

int main () {
	for ( int T = rint (); T --; ) {
		buc.clear ();
		int n = rint ();
		for ( int i = 1; i <= n; ++ i ) ++ buc[rint ()];
		if ( n & 1 ) puts ( "Second" );
		else {
			bool ok = false;
			for ( auto ele: buc ) {
				if ( ele.second & 1 ) {
					puts ( "First" ), ok = true;
					break;
				}
			}
			if ( !ok ) puts ( "Second" );
		}
	}
	return 0;
}

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e10;

int main() {
    ll n;
    cin >> n;

    vvl v(n, vl(5));
    rep(i, n) {
        rep(j, 5) {
            cin >> v[i][j];
        }
    }

    // 判定問題「チームの総合力をx以上にできるか？」を二分探索で解く
    ll left = 0;
    ll right = INF;  /*・INFの値が大きすぎてオーバーフローしていないか？
                    ・INFの値が制約に対して小さすぎないか？         */
    while(right - left > 1) {
        ll x = (left + right) / 2;
        set<ll> st;
        rep(i, n) {
            ll y = 0;
            rep(j, 5) {
                if(v[i][j] >= x) {
                    y |= 1<<j;
                }
            }
            st.insert(y);
        }
        vl w;
        for(ll y : st) {
            w.pb(y);
        }
        bool ok = false;
        ll m = w.size();
        if(m == 1) {
            if(w[0] == (1<<5) - 1) {
                ok = true;
            }
        }
        else if(m == 2) {
            if((w[0] | w[1]) == (1<<5) - 1) {
                ok = true;
            }
        }
        else {
            exrep(i, 0, m-3) {
                exrep(j, i+1, m-2) {
                    exrep(k, j+1, m-1) {
                        if((w[i] | w[j] | w[k]) == (1<<5) - 1) {
                            ok = true;
                        }
                    }
                }
            }
        }
        if(ok) {
            left = x;
        }
        else {
            right = x;
        }
    }

    out(left);
    re0;
}


#include<cstdio>
using namespace std;
long long C(int u,int d) {
	long long ans=1;
	for(long long i=d,num=1; num<=u; num++,i--)
		ans=ans*i/num;
	return ans;
}
long long a,b,k;
int main() {
	scanf("%lld%lld%lld",&a,&b,&k);
	for(; a+b;) {
		if(a==0) {
			b--;
			printf("b");
			continue;
		}
		if(b==0) {
			a--;
			printf("a");
			continue;
		}
		if(k>C(a-1,a+b-1)) {
			k-=C(a-1,a+b-1);
			b--;
			printf("b");
		} else {
			a--;
			printf("a");
		}
	}
	return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
#define P pair<int,ll>
#define MOD 1000000007

using namespace std;

int n, c1,c2, p, res;
vector<int> v1, v2;
string s, t;

int main(){
    cin >> n >> s >> t;
    rep(i, n)if(s[i]=='0'){v1.push_back(i);}
    rep(i, n)if(t[i]=='0'){v2.push_back(i);}
    if(v1.size() != v2.size()){
        cout << -1 << endl;
        return 0;
    }
    rep(i,v1.size()){
        if(v1[i] != v2[i])res++;
    }

    cout << res << endl;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define max_value 0x3f3f3f
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
typedef pair<ll,ll> pii;

const int  inf=1e9+7;
const int mod=998244353;
const int maxn=1e3+1;
ll sqr(ll x){
return x*x;
}
ll modexpo(ll a,ll b){
    a%=inf;
ll res=1;
 while(b>0){
    if(b&1)
        res=res*a%inf;
    a=a*a%inf;
    b>>=1;
 }
 return res;

}

void solve(){
int n;
cin>>n;
string s;
cin>>s;
string t;
cin>>t;
vector<int>v,v1;
for(int i=0;i<n;i++){
    if(s[i]=='0')
        v.push_back(i);
    if(t[i]=='0')
        v1.push_back(i);
}
if((int)v.size()!=(int)v1.size())
cout<<"-1";
else{
        int cnt=0;
    for(int i=0;i<v1.size();i++){
        if(v1[i]!=v[i]){
           cnt++;
        }
    }
   cout<<cnt;
}

}

int main(){
    FIO;
  /*file;*/
 int t;
t=1;
 while(t--){
 solve();
}

}

#include <bits/stdc++.h>

using namespace std;
using ll =long long;
typedef pair<ll,ll> P;
#define SORT(a) sort((a).begin(),(a).end())
#define REV(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

void coY() {cout <<"Yes"<<endl;}
void coN(){cout <<"No"<<endl;}
void mswap(ll &a, ll &b){ if(a >= b) swap(a,b); }
void rswap(ll &a, ll &b){ if(a <= b) swap(a,b); }

const int dy[] = {0,0,1,-1};
const int dx[] = {1,-1,0,0};
const ll mod = 1e9+7;
const ll MOD = 998244353;
const double PI=3.14159265358979323846;
const int inf = 1001001001;
const ll INF = 1e18;
//Write From this Line
int main()
{
	int n, m;
	cin >>  n >> m;
	vector<int> a(m), b(m);
	rep(i,m) cin >> a[i] >> b[i];

	int k;
	cin >>  k;
	vector<int> c(k), d(k);
	rep(i,k) cin >> c[i] >> d[i];

	int ans = 0;
	for(int bit = 0; bit < (1 << k); bit++){
		bitset<16> tmp(bit);
		int now = 0;
		map<int, bool> mp;
		rep(i,k){
			// bit が立っていたらc[i]を使う
			if(tmp.test(i)){
				mp[c[i]] = true;
			} else {
				mp[d[i]] = true;
			}
		}
		// 何個の条件を満たすことが出来ているか
		rep(i,m){
			if(mp[a[i]] && mp[b[i]]){
				now++;
			}
		}
		chmax(ans,now);
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int N;
    cin >> N;
    vector<int> x(N), y(N);
    for(int i = 0; i < N; i++) cin >> x[i] >> y[i];
    int cnt = 0;
    for(int i = 0; i < N; i++){
        for(int j = i + 1; j < N; j++){
            int t = abs(y[j] - y[i]);
            int s = abs(x[j] - x[i]);
            if(-s <= t && t <= s) cnt++;
        }
    }
    cout << cnt << endl;
}

#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <memory.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <ctime>
#include <iostream>
#include <functional>
#include <complex>
#include <stdlib.h>
#include <fstream>
#include <random>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<pii, int> p3i;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<p3i> v3i;
typedef vector<vii> vvii;
typedef vector<p3i> vp3i;
typedef long double ld;
typedef vector<ld> vld;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define REPD(i, n) for (int (i) = (n) - 1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define FORD(i,a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rv(v) reverse(all(v))
#define CL(v, val) memset((v), (val), sizeof((v)))
#define SORT(a) sort(all(a))
#define un(v) SORT(v), (v).resize(unique(all(v)) - (v).begin())
#define eps 1.0e-9
#define X first
#define Y second
#define bit(n) (1 << (n))
#define bit64(n) (ll(1) << (n))
#define sqr(x) ((x) * (x))
#define sq5(x) ((x) * (x) * (x) * (x) * (x))
#define N 1005

int x[N], y[N];

int main(void) {
	int n;
	scanf("%d", &n);
	REP(i, n) {
		scanf("%d%d", &x[i], &y[i]);
	}

	int cnt = 0;
	REP(i, n) {
		FOR(j, i + 1, n) {
			pii a(x[i], y[i]), b(x[j], y[j]);
			double res = 1.0 * (b.Y - a.Y) / (b.X - a.X);
			if (fabs(res) - 1 < eps) {
				cnt++;
			}
		}
	}

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


#include <bits/stdc++.h>
using namespace std;
#define REP(i, e) for (int i = 0; i < (e); ++i)
#define REP3(i, b, e) for (int i = (b); i < (e); ++i)
#define RREP(i, b, e) for (int i = (b)-1; i >= e; --i)

struct Initializer {
  Initializer() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(0);
    std::cout << std::fixed << std::setprecision(15) << std::boolalpha;
  }
} initializer;

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
  for (auto &x : vec) {
    os << x;
    if (&x != &vec.back()) os << ' ';
  }
  return os;
}

template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &pa) {
  return os << pa.first << ' ' << pa.second;
}

template <std::size_t N, typename... T>
std::ostream &print_tuple(std::ostream &os, const std::tuple<T...> &tup) {
  if constexpr (N != 1UL) {
    print_tuple<N - 1, T...>(os, tup);
    os << ", " << std::get<N - 1>(tup);
  } else {
    os << std::get<0>(tup);
  }
  return os;
}

template <typename... T>
std::ostream &operator<<(std::ostream &os, const std::tuple<T...> &tup) {
  os << '(';
  print_tuple<sizeof...(T), T...>(os, tup);
  return os << ')';
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::set<T> &se) {
  os << '{';
  for (auto &x : se) {
    os << x;
    if (&x != &*se.rbegin()) os << ", ";
  }
  return os << '}';
}

template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const std::map<T, U> &ma) {
  os << '{';
  for (auto &x : ma) {
    os << x.first << ": " << x.second;
    if (&x != &*ma.rbegin()) os << ", ";
  }
  return os << '}';
}

inline void print(void) {
  std::cout << '\n';
}

template <class T, class... U>
inline void print(const T &x, const U &...y) {
  std::cout << x;
  if (sizeof...(U) != 0) {
    std::cout << ' ';
  }
  print(y...);
}

int n;
map<int, int> ma;
vector<int> c;
vector<vector<int>> g;
vector<int> ans;

void rec(int par, int cur) {
  if (ma[c[cur]] == 1) ans.emplace_back(cur);

  for (auto nx : g[cur]) {
    if (nx == par) continue;
    ++ma[c[nx]];
    rec(cur, nx);
    --ma[c[nx]];
  }
}

int main() {
  cin >> n;
  c.resize(n);
  REP(i, n) cin >> c[i];
  g.resize(n);
  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  ++ma[c[0]];
  rec(-1, 0);
  sort(ans.begin(), ans.end());
  REP(i, ans.size()) { print(ans[i] + 1); }

  return 0;
}

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include<bits/stdc++.h>
#define ll int64_t
#define ff first
#define ss second
#define mkp make_pair
#define pb push_back
#define pf push_front
#define inf 100000000
#define maxn 100005
#define mod  998244353
#define mod1 1000000009

using namespace std;

int col[maxn];

int colour[maxn];

vector<int>v[maxn];
vector<int>ans;

void dfs(int src,int par){
   if(colour[col[src]]==0){
     ans.push_back(src);
   }
   colour[col[src]]++;

   for(auto it:v[src]){
    if(it==par)
      continue;
    dfs(it,src);
   }

   colour[col[src]]--;
}

void solve(){
  int n;
  cin>>n;

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

  int a,b;

  for(int i=0;i<n-1;++i){
     cin>>a>>b;
     v[a].pb(b);
     v[b].pb(a);
  }

  dfs(1,-1);

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

  for(int i=0;i<ans.size();++i)
     cout<<ans[i]<<'\n';

}

int main(){

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

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

  solve();

return 0;

}


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

int main(){
  int64_t n;
  cin>>n;
  vector<int>ans(n+1,1);
  for(int i=1;i<=n;i++){
    for(int j=i*2;j<=n;j+=i){
      ans[j]=max(ans[j],ans[i]+1);
    }
  }
  for(int i=1;i<=n;i++)cout<<ans[i]<<' ';puts("");
}

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long LL;

LL solve(long long N) {
	LL N2 = N * 2, ans = 0;
	for (LL i = 1; i * i <= N2; ++i) {
		LL sum = (i * (i - 1)) / 2;
		ans += (abs(N - sum) % i) == 0;
	}
	return ans * 2;
}

int main() {
    long long N;
	std::cin >> N;
	cout << solve(N) << endl;
	return 0;
}


#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
//#define fi first
//#define se second
#define debug(x) cout << #x << ": " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> void Print(vector<T> v) {
  F0R(i, v.size()) {
    cout << v[i] << ' ';
  }
  cout << newl;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, K, M;
  cin >> N >> K >> M;

  int sum = 0;
  F0R(i, N - 1) {
    int a; cin >> a;
    sum += a;
  }

  int d = max(N * M - sum, 0);

  cout << (d > K ? -1 : d);
}
#endif

#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define ini(x, y) memset(x, y, sizeof(x));
#define pb push_back
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = (int)1e9 + 7;
const int MAX = (int)2e9;
const double PI = acos(-1);
ll exEuclid(ll a, ll b, ll& s, ll& t) {
	if (b == 0) {
		s = 1; t = 0;
		return a;
	}
	ll gcd = exEuclid(b, a % b, s, t);
	ll tmp = t;
	t = s - (a / b) * t;
	s = tmp;

	if (s <= 0) { //s를 양수로
		s += b;
		t -= a;
	}
	return gcd;
}
int main(void) {
	ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	//	freopen("input.txt", "r", stdin);
	int T; cin >> T;
	while (T--) {
		ll N, S, K; cin >> N >> S >> K;
		ll a, x;
		ll g = exEuclid(N, K, a, x);
		if (S % g != 0) cout << -1 << '\n';
		else {
			a *= S / g;
			x *= S / g;
			ll k = (x + 1) / (N / g);
			x -= k * (N / g);
			a += k * (K / g);

			cout << -x << '\n';
		}
	}
}


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

int main() {
	int n, cnt1, cnt2, m;
	cin >> n >> m;
	cnt1 = cnt2 = 0;
	for (int i = 1; i <= n; i++) {
		string s;
		cin >> s;
		int cnt = 0;
		for (auto p : s) p == '1' ? cnt++ : 0;
		cnt & 1 ? cnt1++ : cnt2++;
	}
	cout << 1ll * cnt1 * cnt2 << endl;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vv = vector<vi>;
const ll INF = 1001001001001001001;

int main(){
    int n, m;
    cin >> n >> m;
    vi a(2, 0);
    rep(i,n){
        string s;
        cin >> s;
        int num = 0;
        rep(j,m)num += s[j]-'0';
        a[num%2]++;
    }

    cout << a[0]*a[1] << endl;

    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    int A, B;
    cin >> A >> B;
    vector<int> sum(1001,0);
    for(int i=1; i<=1000; i++){
        sum[i] = sum[i-1] + i;
    }
    if(A==B){
        for(int i=1; i<=A; i++) cout << i << " " << -i << " ";
    }
    else if(A>B){
        for(int i=1; i<=A; i++) cout << i << " ";
        //sum[A]
        int near = 2147483641;
        int left, right;
        for(int i=0; i<=1000-B; i++){
            int j = i + B;
            int diff = sum[A] - (sum[j]-sum[i]);
            if(diff>0){
                near = min(near, diff);
                if(near == diff){
                    left = i;
                    right = j;
                }
            }
        }
        for(int i=left+1; i<right; i++) cout << -i << " ";
        cout << -right - (sum[A] - sum[right] + sum[left]);
    }
    else{
        for(int i=1; i<=B; i++) cout << -i << " ";
        //sum[B]
        int near = 2147483641;
        int left, right;
        for(int i=0; i<=1000-A; i++){
            int j = i + A;
            int diff = sum[B] - (sum[j]-sum[i]);
            if(diff>0){
                near = min(near, diff);
                if(near == diff){
                    left = i;
                    right = j;
                }
            }
        }
        for(int i=left+1; i<right; i++) cout << i << " ";
        cout << right + (sum[B] - sum[right] + sum[left]);
    }
    cout << endl;
}

#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, s, n) for (int i = s; i <= (int)(n); i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define vec vector
#define rip return 0
typedef long long ll;
typedef pair<ll,ll> P;
ll max(ll x,ll y){
if(x>y)return x;
else return y;
}
ll min(ll x,ll y){
if(x<y)return x;
else return y;
}

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

  if(a%2==0){
    rep(i,a){
      if(i<(a/2))cout<<10000*b+a/2-i<<" ";
      else cout<<10000*b+a/2-i-1<<" ";
    }
  }
  else{
    rep(i,a)cout<<10000*b+a/2-i<<" ";
  }
  if(b%2==0){
    rep(i,b){
      if(i<(b/2))cout<<-10000*a+b/2-i<<" ";
      else cout<<-10000*a+b/2-i-1<<" ";
    }
  }
  else{
    rep(i,b)cout<<-10000*a+b/2-i<<" ";
  }
  cout<<endl;
}


#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
inline int read() {
	int x=0,f=1;char c=getchar();
	while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
	while(c>=48&&c<=57)x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
using namespace std;
const int M=1005;
struct node {
	int to,c;
};
vector <node> E[M];
bool vis[M][M];
struct PR {
	int l,r,dis;
	bool operator <(const PR &a)const {return dis>a.dis;}
};
priority_queue <PR> Q;
int main() {
	int n=read(),m=read();
	char s[5];
	rep(i,1,m){
		int u=read(),v=read();scanf("%s",s+1);
		E[u].push_back((node){v,s[1]});
		E[v].push_back((node){u,s[1]});
		Q.push((PR){u,v,1});
		Q.push((PR){v,u,1});
	}
	rep(i,1,n)Q.push((PR){i,i,0});
	while(!Q.empty()){
		PR top=Q.top();Q.pop();
		if(top.l==1&&top.r==n){
			printf("%d\n",top.dis);
			return 0;
		}
		if(vis[top.l][top.r])continue;
		vis[top.l][top.r]=1;
		rep(i,0,E[top.l].size()-1){
			int to=E[top.l][i].to,c=E[top.l][i].c;
			rep(j,0,E[top.r].size()-1){
				int t=E[top.r][j].to,cc=E[top.r][j].c;
				if(c!=cc)continue;
				Q.push((PR){to,t,top.dis+2});
			}
		}
	}
	puts("-1");
	return 0;
}

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

typedef pair<int,int> pii;
typedef pair<string,int> psi;
typedef pair<int,string> pis;
typedef array<int,2> aii;
typedef array<int,3> aiii;
typedef array<int,4> aiiii;
typedef unsigned long long ull;
typedef long long int ll;
typedef array<ll,2> all;
typedef array<ll,3> alll;
typedef array<ll,4> allll;

#define pb push_back
#define ff first
#define ss second
#define MAX 1000005
#define MOD 1000000007
#define INF 1e9+100
vector<int>adj[MAX];
int xMove[] = { 0,1,0,-1,1,1,-1,-1, 2, 2, -2, -2, 1, 1, -1, -1 };
int yMove[] = { 1,0,-1,0,1,-1,1,-1, 1, -1, 1, -1, 2, -2, 2, -2 };

void add_edge(int a, int b, int c, int d, int n)
{
    int u=(a-1)*n+b,v=(c-1)*n+d;
    adj[u].pb(v);
    adj[v].pb(u);
}

int dist[MAX];

void bfs(int s)
{
    memset(dist,-1,sizeof dist);

    queue<int> q;
    q.push(s);
    dist[s]=0;

    while(!q.empty())
    {
        int u=q.front();
        q.pop();

        for(int v: adj[u])
        {
            if(dist[v]==-1)
            {
                dist[v]=dist[u]+1;
                q.push(v);
            }
        }
    }
}

int main()
{
    //freopen("input.in","r",stdin);
    //freopen("output.txt","w",stdout);
    cin.tie(0),cout.tie(0);
    ios_base::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(10);
    int TC=1;
    int n,m,k,q;
    //cin>>TC;
    for(int t1=1; t1<=TC; t1++)
    {
        cin>>n>>m;
        vector<aiii> edges(m);
        int mp[n*n+1];
        memset(mp,-1,sizeof mp);
        for(auto &[u,v,c]: edges)
        {
            char c1;
            cin>>u>>v>>c1;
            c=c1;
            mp[(u-1)*n+v]=1;
            mp[(v-1)*n+u]=1;
        }

        for(auto [a,b,c1]: edges)
        {
            for(auto [c,d,c2]: edges)
            {
                if(c2==c1)
                {
                    add_edge(a,c,b,d,n);
                    add_edge(c,a,d,b,n);
                    add_edge(a,d,b,c,n);
                    add_edge(d,a,c,b,n);
                }
            }
        }

        bfs(n);
        int mn=INF;
        for(int i=1;i<=n*n;i++)
        {
            if(dist[i]!=-1)
            {
                int r=i%n;
                int d=(i-r)/n;
                if(d+1==r)
                    mn=min(mn,2*dist[i]);
                else if(mp[i]==1)
                    mn=min(mn,2*dist[i]+1);
            }
        }

        cout<<(mn==INF?-1:mn)<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
const ll MOD = 1e9+7;

bool sprawdz(vector<vector<int>> & grid, int n, int k, int value){
	vector<vector<int>> edit(n+1, vector<int>(n+1));
	vector<vector<int>> sums(n+1, vector<int>(n+1, 0));
	for(int i = 1; i<=n; i++){
		for(int j = 1; j<=n; j++){
			if(grid[i][j] <= value){
				edit[i][j] = 1;
			}
			else{
				edit[i][j] = 0;
			}
			sums[i][j] = sums[i-1][j] + sums[i][j-1] - sums[i-1][j-1] + edit[i][j];
		}
	}
	for(int i = k; i<=n; i++){
		for(int j = k; j<=n; j++){
			if(sums[i][j] - sums[i-k][j] - sums[i][j-k] + sums[i-k][j-k] >= (k*k+1)/2 ){
				return true;
			}
		}
	}
	return false;
}

void solve()
{
	int n, k;
	cin >> n >> k;
	vector<vector<int>> grid(n+1, vector<int>(n+1));
	int MIN = INT_MAX;
	int MAX = 0;
	for(int i = 1; i<=n; i++){
		for(int j = 1; j<=n; j++){
			cin >> grid[i][j];
			MIN = min(MIN, grid[i][j]);
			MAX = max(MAX, grid[i][j]);
		}
	}
	int start = MIN, end = MAX;
	while(start < end){
		int mid = (start+end)/2;
		if(sprawdz(grid, n, k, mid)){
			end = mid;
		}
		else{
			start = mid+1;
		}
	}
	cout << start << endl;

}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	bool testcases = false;
	if (testcases)
	{
		int t;
		cin >> t;
		while (t--)
		{
			solve();
		}
	}
	else
	{
		solve();
	}
	return 0;
}

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

typedef long long int ll;
#define deb(x) cout << #x << " " << x << endl;
#define debtwo(x,y) cout << x << ", " << y << endl;
#define mod 1000000007
#define N 1000001

void solve() {
	ll n, m;
	cin >> n >> m;
	vector<ll> arr(n), brr(m);
	for (ll &x : arr) cin >> x;
	for (ll &x : brr) cin >> x;

	ll dp[n + 1][m + 1];
	memset(dp, 0, sizeof dp);

	for (int j = 1; j <= m; j++) dp[0][j] = j;

	for (int i = 1; i <= n; i++) {
		dp[i][0] = i;
		for (int j = 1; j <= m; j++) {
			if (arr[i - 1] == brr[j - 1]) {
				dp[i][j] = min({1 + dp[i][j - 1], 1 + dp[i - 1][j], dp[i - 1][j - 1]});
			}
			else {
				dp[i][j] = min({1 + dp[i][j - 1], 1 + dp[i - 1][j], 1 + dp[i - 1][j - 1]});
			}
		}
	}
	// cout << "x   ";
	// for (int i = 1; i <= m; i++) cout << brr[i - 1] << " "; cout << endl;
	// for (int i = 0; i <= n; i++) {
	// 	if (i != 0) cout << arr[i - 1] << " ";
	// 	else cout << "  ";
	// 	for (int j = 0; j <= m; j++) {
	// 		cout << dp[i][j] << " ";
	// 	}
	// 	cout << endl;
	// }

	cout << dp[n][m];
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	ll t = 1;
	//cin >> t;

	while (t--) {
		solve();
	}
}


#include <iostream>
#include <fstream>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>

#define pb push_back
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(),(a).end()
#define mp make_pair

using namespace std;

typedef long long int64;
typedef vector<int> vi;
typedef pair<int,int> ii;
//https://codeforces.com/problemset/page/7?order=BY_RATING_ASC//
class A
{
public:
void solveOne(istream &in,ostream &out)
{
int a,b,c,d;
in>>a>>b;
in>>c>>d;
out<<a*d-b*c<<"\n";
}
void solve(std::istream &in,std::ostream &out)
{
int tc=1;
//in>>tc;
for(int i=0;i<tc;++i)
{
solveOne(in,out);
}
}

};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
A a;
std::istream& in(std::cin);
std::ostream& out(std::cout);
a.solve(in,out);
return 0;
}

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

int main() {
  int64_t A, B, C;
  cin >> A >> B >> C;

  C = C % 2;
  if(C==0){C+=2;}

  if(C==2){
    A*=A;
    B*=B;
  }

  if (A > B)
    cout << '>' << endl;
  else if (A < B)
    cout << '<' << endl;
  else
    cout << '=' << endl;
}


#include<bits/stdc++.h>
#include<tuple>
typedef long long ll;
#define INF 99999999999999
#define mod 1000000007
#define eps 1e-9
using namespace std;
ll n,h,l=1,r=1000000000;
ll val[10][3005];
ll ans,res;
set<ll>bsk;
map<ll,ll>cnt;
bool check(ll x){
    bsk.clear();
    ll can=0;
    //cout<<"x="<<x<<endl;
    for(ll i=1;i<=n;i++){
        ll res=0;
        for(ll j=0;j<=4;j++)
            if(val[j][i]>=x)
                res+=1<<j;
        //cout<<res<<endl;
        bsk.insert(res);
    }
    for(auto i:bsk)
        for(auto j:bsk)
            for(auto k:bsk){
                if((i|j|k)==31)
                    can=1;
                }
    return can;
}
int main(){
    ios::sync_with_stdio(0);
    auto st=clock();
    cin>>n;
    srand(time(NULL));
    for(ll i=1;i<=n;++i)
        for(ll j=0;j<=4;++j)
            cin>>val[j][i];
    while(l<=r){
        ll mid=(l+r)>>1;
        if(check(mid))
            ans=mid,l=mid+1;
        else
            r=mid-1;
    }
    cout<<ans<<endl;
    return 0;
}

#include <bits/stdc++.h>
#define li long int
#define lli long long
#define pb push_back
#define fi first
#define se second
#define si size()
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define lb lower_bound
#define ub upper_bound
#define BS binary_search
#define pll pair < lli, lli >
#define pii pair < int, int>
#define vi vector < int >
#define vvi vector < vector < int > >
#define vlli vector < lli >
#define all(x) x.begin(), x.end()
using namespace std;

int main()
{
    fastio;
    int mid,ans=1,n,i,l,r,j,f=0,k;
    cin>>n;
    vvi v(n, vi(5)), arr(n, vi(5));
    for(i=0;i<n;i++)    cin>>v[i][0]>>v[i][1]>>v[i][2]>>v[i][3]>>v[i][4];
    l=1, r=1e9;
    while(l<=r)
    {

        arr=v;
        mid=(l+r)/2;
        f=0;
        set<int> s;
        for(i=0;i<n;i++)
        {
            for(j=0;j<5;j++)
            {
                if(arr[i][j]<mid)   arr[i][j]=0;
                else                arr[i][j]=1;
            }
            int zz=arr[i][0]*16+arr[i][1]*8+arr[i][2]*4+arr[i][3]*2+arr[i][4];
            if(zz==31)  {f=1;}
            s.insert(zz);
        }
        if(f)
        {
            l=mid+1;
            ans=max(ans, mid);
            continue;
        }
        vi pos;
        for(auto ii:s)  pos.pb(ii);
        int sz=s.size();
        if(sz==1)
        {
            r=mid-1;    continue;
        }
        if(sz==2)
        {
            if((pos[0]|pos[1])==31)
            {
                l=mid+1;ans=max(ans, mid);
            }
            else    r=mid-1;
            continue;
        }
        for(i=0;i<sz-2;i++)
        {
            for(j=i+1;j<sz-1;j++)
            {
                for(k=j+1;k<sz;k++)
                {
                    if((pos[i]|pos[j]|pos[k])==31){f=1; break;}
                }
            }
        }
        if(f)  { l=mid+1;ans=max(ans, mid);}
        else    r=mid-1;
    }
    cout<<ans<<"\n";
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(15);}}init_;

#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//

int main() {
    ll N;
    cin >> N;

    if(N == 3){
        cout << "6 10 15" << endl;
        return 0;
    }

    vl cand;
    repe(i, 1, 10001){
        int d2 = i % 2 == 0, d3 = i % 3 == 0, d5 = i % 5 == 0;
        if (d2 + d3 + d5 >= 2) cand.eb(i);
    }

    // vl ans(N);
    rep(i, N) cout << cand[i] << " ";
    cout << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef complex<double> point;
#define endl '\n'
#define F first
#define S second
#define dbg(x) cerr<<#x<<" = "<<x<<endl
#define dbg2(x,y) cerr<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<endl
#define dbg3(x,y,z) cerr<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<", "<<#z<<" = "<<z<<endl
#define dbg4(x,y,z,q) cerr<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<", "<<#z<<" = "<<z<<", "<<#q<<" = "<<q<<endl
#define FOR(sz) for(int i = 0; i < sz; ++i)
#define dbg_array(x,sz) FOR(sz) cerr << x[i] << " \n"[i==sz-1]
#define X real()
#define Y imag()

const int d4i[4] = { -1, 0, 1, 0 }, d4j[4] = { 0, 1, 0, -1 };
const int d8i[8] = { -1, -1, 0, 1, 1, 1, 0, -1 }, d8j[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };

void _init_() {
	ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef ECLIPSE
	freopen("input.txt", "rt", stdin);
//	freopen("output.txt", "wt", stdout);
#endif
}

const int N = 2e4 + 9, M = 1e8 + 9, SEG = 131072; // SEG = 1 << ((int) ceil(log2(N)) + 1)
const int Mod_M = 1e9 + 7;

int n;
vector<int> ans;
int vis[N], vid = 1;
void generate(int num, int a, int b) {
	if (int(ans.size()) == n) return;
	if (num > 10000) return;
	if (vis[num] != vid) ans.push_back(num);
	vis[num] = vid;
	generate(num * a, a, b);
	generate(num * b, a, b);
}

vector<int> sieve(int n) {
	vector<bool> is_prime(n + 1, true);
	vector<int> prime;
	for (int i = 2; i <= n; ++i) {
		if (!is_prime[i]) continue;
		else {
			prime.push_back(i);
			for (int j = 2; i * j <= n; ++j) {
				is_prime[i * j] = 0;
			}
		}
	}
	return prime;
}

ll GCD(ll a, ll b) { // a>b
	if (b == 0) return a;
	return GCD(b, a % b);
}

bool checker() {
	for (int i = 0; i < int(ans.size()); ++i) {
		for (int j = i + 1; j < int(ans.size()); ++j) {
			ll gcd;
			if (ans[i] > ans[j]) gcd = GCD(ans[i], ans[j]);
			else if (ans[i] < ans[j]) gcd = GCD(ans[j], ans[i]);
			else {
				dbg2(ans[i], ans[j]);
				return false;
			}

			if (gcd == 1) {
				return false;
			}
		}
	}
	ll gcd = ans[0];
	for (int i = 1; i < int(ans.size()); ++i) {
		gcd = GCD(gcd, ans[i]);
	}

	return gcd == 1;
}
void solve() {
//	vector<int> prime = sieve(1000);
	scanf("%d", &n);
	ans.push_back(6);
	ans.push_back(10);
	ans.push_back(15);
//	vis[6] = vid;
//	vis[10] = vid;
//	vis[15] = vid;
//	generate(6, 2, 3);
//	generate(10, 2, 5);
//	generate(15, 5, 3);

//	int num = 2 * 3, k = 3, num2 = num;
//	while (num2 < 10000) {
//		for (int i = k; i < int(prime.size()); ++i) {
//			int tmp = num2 * prime[i];
//			if (tmp > 10000 || int(ans.size()) == n) break;
//			generate(tmp, 2, 3);
//			generate(tmp, 2, prime[i]);
//			generate(tmp, 3, prime[i]);
//			generate(tmp, 2, prime[k - 1]);
//			generate(tmp, 3, prime[k - 1]);
//		}
//		num2 = num * prime[k];
//		++k;
//	}
	for (int i = 16; i < 10001; ++i) {
		if (GCD(i, 6) > 1 && GCD(i, 10) > 1 && GCD(i, 15) > 1) ans.push_back(i);
	}
	dbg(ans.size());
	dbg(checker());
	for (int i = 0; i < n; ++i)
		printf("%d%c", ans[i], " \n"[i == int(ans.size()) - 1]);
}

int main() {
	_init_();
//	int t;
//	scanf("%d", &t);
//	while (t--) {
	solve();
//	}
	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
constexpr int MX = 5005, MOD = 998244353;

pl DP[MX][2];

int main() {
	if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout);
	cin.tie(0)->sync_with_stdio(0);

	int N, M; cin >> N >> M;
	ll ans = 0;
	for (int i = 1; i <= M; ++i) {
		DP[0][0] = pl(0, M-1), DP[0][1] = pl(1, 1);
		for (int j = 0; j < N-1; ++j) {
			DP[j+1][0] = pl((M-1)*DP[j][0].f+(i-1)*DP[j][1].f, (M-1)*DP[j][0].s+(i-1)*DP[j][1].s);
			DP[j+1][1] = pl(DP[j][0].f+DP[j][0].s+(M-i+1)*DP[j][1].f, DP[j][0].s+(M-i+1)*DP[j][1].s);
			DP[j+1][0].f %= MOD, DP[j+1][0].s %= MOD;
			DP[j+1][1].f %= MOD, DP[j+1][1].s %= MOD;
		}
		(ans += DP[N-1][0].f+DP[N-1][1].f) %= MOD;
	}
	cout << ans;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll z = 0;

int main(){
  ll H, W;
  cin >> H >> W;

  vector<vector<char>> S(H + 1, vector<char>(W + 1));
  for (ll i = 1; i <= H; i++){
    for (ll j = 1; j <= W; j++){
      cin >> S[i][j];
    }
  }

  vector<vector<ll>> Wtate(H + 1, vector<ll>(W + 1)), Wyoko(H + 1, vector<ll>(W + 1)), Wnaname(H + 1, vector<ll>(W + 1));
  for (ll i = 1; i <= H; i++){
    ll now = 0;
    for (ll j = 1; j <= W; j++){
      if (S[i][j] == '#'){
        now = j;
      }

      Wtate[i][j] = now;
    }
  }
  for (ll j = 1; j <= W; j++){
    ll now = 0;
    for (ll i = 1; i <= H; i++){
      if (S[i][j] == '#'){
        now = i;
      }

      Wyoko[i][j] = now;
    }
  }
  for (ll i = H; i > 0; i--){
    ll now = 0;
    for (ll j = 1; j <= W; j++){
      if (i + j > H || j > W){
        break;
      }

      if (S[i + j][j] == '#'){
        now = j;
      }

      Wtate[i + j][j] = now;
    }
  }
  for (ll j = 2; j <= W; j++){
    ll now = 0;
    for (ll i = 1; i <= H; i++){
      if (i + j > W || i > H){
        break;
      }

      if (S[i][i + j] == '#'){
        now = i;
      }

      Wtate[i][i + j] = now;
    }
  }

  vector<vector<ll>> ruitate(H + 1, vector<ll>(W + 1, 0)), ruiyoko(H + 1, vector<ll>(W + 1, 0)), ruinaname(H + 1, vector<ll>(W + 1, 0)), ans(H + 1, vector<ll>(W + 1, 0));
  for (ll i = 1; i <= H; i++){
    for (ll j = 1; j <= W; j++){
      if (i == 1 && j == 1){
        ruitate[i][j] = 1;
        ruiyoko[i][j] = 1;
        ruinaname[i][j] = 1;
        ans[i][j] = 1;
        continue;
      }

      if (S[i][j] == '.'){
        ans[i][j] += ruitate[max(z, i - 1)][j];
        ans[i][j] += ruiyoko[i][max(z, j - 1)];
        ans[i][j] += ruinaname[max(z, i - 1)][max(z, j - 1)];

        ans[i][j] %= 1000000007;
        ruitate[i][j] = (ans[i][j] + ruitate[max(z, i - 1)][j]) % 1000000007;
        ruiyoko[i][j] = (ans[i][j] + ruiyoko[i][max(z, j - 1)]) % 1000000007;
        ruinaname[i][j] = (ans[i][j] + ruinaname[max(z, i - 1)][max(z, j - 1)]) % 1000000007;
      }
    }
  }

  cout << ans[H][W] << endl;
}


#include<bits/stdc++.h>
using namespace std;
long long t,l,r;
int main() {
	cin>>t;
	while(t--) {
		cin>>l>>r;
		if(r-2*l+1>0) cout<<(r-2*l+1)*(r-2*l+2)/2<<endl;
		else cout<<0<<endl;
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define reps(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define repit(i,C) for (auto i = (C).begin(); i != (C).end(); i++)
#define pr(a) cout << a
#define prl(a) cout << (a) << endl
#define prld(a) cout << setprecision(15)<< (a) << endl
#define allrange(a) a.begin(),a.end()

bool solve_translated(vector<pair<int,int>> &S,vector<pair<int,int>> &T){
    int N = S.size();
    int dx = S[0].first-T[0].first;
    int dy = S[0].second-T[0].second;
    bool flg = true;
    reps(i,1,N){
        if(!((S[i].first==T[i].first+dx) && (S[i].second==T[i].second+dy))){flg = false; break;}
    }
    return flg;
}
/*ピタゴラ三角形
    5	12	13
    8	15	17
    3	4	5
*/
vector<pair<int,int>> Pitagora_rot(vector<pair<int,int>> &S,int a, int b , int c){
    int N = S.size();
    vector<pair<int,int>> PS(N);
    auto pt0 = S[0];
    PS[0] = pt0;
    bool flg = true;
    reps(i,1,N){
        int dx = S[i].first - pt0.first;
        int dy = S[i].second - pt0.second;
        if(((dx*b+dy*a)%c)!=0 || ((dx*a-dy*b)%c)!=0) {flg = false; break;}
        PS[i] = make_pair((dx*a-dy*b)/c+pt0.first, (dx*b+dy*a)/c+pt0.second);
    }
    if(flg) return PS;
    else return vector<pair<int,int>>();
}

int main(){
    std::cin.tie(0); // cinとcoutの同期を解除
    std::ios::sync_with_stdio(false);
    int N;cin >> N;

    vector<pair<int,int>> S(N),T(N);
    rep(i,N)
    {
        int x,y;
        cin >> x >> y;
        S[i].first = x;S[i].second = y;
    }

    rep(i,N)
    {
        int x,y;
        cin >> x >> y;
        T[i].first = x;
        T[i].second = y;
    }
    sort(allrange(T));

/*ピタゴラ三角形
    5	12	13
    8	15	17
    3	4	5
*/
    vector<vector<int>> tri(12);
    tri[0]={0,1,1};
    tri[1]={-1,0,1};
    tri[2]={0,-1,1};
    tri[3]={1,0,1};
    tri[4]={3,4,5};
    tri[5]={4,3,5};
    tri[6]={-3,4,5};
    tri[7]={-4,3,5};
    tri[8]={-3,-4,5};
    tri[9]={-4,-3,5};
    tri[10]={3,-4,5};
    tri[11]={4,-3,5};
/*
    vector<vector<int>> tri(0);
    tri.push_back(vector<int>({0,1,1}));
    tri.push_back(vector<int>({-1,0,1}));
    tri.push_back(vector<int>({0,-1,1}));
    tri.push_back(vector<int>({1,0,1}));
    tri.push_back(vector<int>({3,4,5}));
    tri.push_back(vector<int>({4,3,5}));
    tri.push_back(vector<int>({-3,4,5}));
    tri.push_back(vector<int>({-4,3,5}));
    tri.push_back(vector<int>({-3,-4,5}));
    tri.push_back(vector<int>({-4,-3,5}));
    tri.push_back(vector<int>({3,-4,5}));
    tri.push_back(vector<int>({4,-3,5}));
*/
 //   tri.push_back(vector<int>({-5, -12, 13}));
 //   tri.push_back(vector<int>({-12, -5, 13}));
 //   tri.push_back(vector<int>({-8,-15,17}));
 //   tri.push_back(vector<int>({-15,-8,17}));
 //   tri.push_back(vector<int>({5, 12, 13}));
 //   tri.push_back(vector<int>({12, 5, 13}));
 //   tri.push_back(vector<int>({8,15,17}));
 //   tri.push_back(vector<int>({15,8,17}));
 //   tri.push_back(vector<int>({-5, 12, 13}));
 //  tri.push_back(vector<int>({-12, 5, 13}));
  //  tri.push_back(vector<int>({-8,15,17}));
  //  tri.push_back(vector<int>({-15,8,17}));
 //   tri.push_back(vector<int>({5, -12, 13}));
 //   tri.push_back(vector<int>({12, -5, 13}));
 //   tri.push_back(vector<int>({8,-15,17}));
 //   tri.push_back(vector<int>({15,-8,17}));

    bool flg;

    if(S.size()==2 && T.size()==2){
        auto sx = S[0].first-S[1].first;
        auto sy = S[0].second-S[1].second;
        auto tx = T[0].first-T[1].first;
        auto ty = T[0].second-T[1].second;
        flg= (sx*sx+sy*sy==tx*tx+ty*ty);
    } else {
        rep(j,tri.size()){
            auto S2 = Pitagora_rot(S,tri[j][0],tri[j][1],tri[j][2]);
            if(S2.empty()) continue;
            sort(allrange(S2));
            flg = solve_translated(S2,T);
            if(flg) break;
        }
    }
    if(flg) prl("Yes"); else prl("No");
}


#include<iostream>
using namespace std;
int N;
main()
{
	cin>>N;
	for(int i=0;i<N;i++)
	{
		cout<<i/2+1<<" "<<(i+N)/2+1<<endl;
	}
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for (int i = 0; i < n; i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
template<class T> ostream& operator << (ostream &s, vector<T> &P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
using P  = pair<int,int>;

void slv(){
    int n;cin>>n;
    vector<vector<int>> divisor(n + 1,vector<int>{});
    rep1(i,n){
        for (int j = i; j <= n; j += i){divisor[j].push_back(i);}
    }
    vector<int> color(n);
    vector<P> A;
    rep1(i,n){int l = divisor[i].size();A.push_back(P{l,i});}
    sort(A.begin(),A.end());
    for (auto [dcnt,v] : A){
        set<int> S;
        for (auto div : divisor[v]) S.insert(color[div - 1]);
        int m = *S.rbegin();
        m++;
        color[v - 1] = m;
    }
    cout << color << endl;
    return;
}

signed main(){
    int t;t = 1;
    rep(i,t){slv();}
    return 0;
}