#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#define ll long long
#define pb push_back
#define forn(i,x,n) for(int i=x;i<=n;++i)
#define forr(i,x,n) for(int i=n;i>=x;--i)
#define lson (rt<< 1)
#define rson (rt<< 1 | 1)
#define gmid ((l+r)>> 1 )
using namespace std;
const int maxn=20000050;

struct node 
{
	int v;int pos;
}aa[maxn],bb[maxn];

int n,s[maxn],a[maxn],b[maxn],c[maxn];

bool cmp(node a,node b)
{
	return a.v<b.v||a.v==b.v&&a.pos<b.pos;
}

int lowbit(int x)
{
	return (x&-x); 
}

int query(int x)
{
	int ans=0;
	while(x)
	{
		ans+=s[x];
		x-=lowbit(x);
	}
	return ans;
}

void insert(int x)
{
	while(x<=n)
	{
		s[x]++;
		x+=lowbit(x);
	}
}

int main()
{
	cin>>n;
	forn(i,1,n)
	  {
	  	  cin>>a[i];
	  	  aa[i].v=a[i]+i;
	  	  aa[i].pos=i;
	  }
	forn(i,1,n)
	  {
	  	  cin>>b[i];
	  	  bb[i].v=b[i]+i;
	  	  bb[i].pos=i;
	  }  
	sort(aa+1,aa+n+1,cmp);
	sort(bb+1,bb+n+1,cmp);
	bool flag=true;
	for(int i=1;i<=n;++i)
	  if(aa[i].v!=bb[i].v)flag=false;
	if(flag==false) cout<<-1<<endl;
	else
	{
		ll ans=0;
		forn(i,1,n)
		  c[aa[i].pos]=bb[i].pos;
		forn(i,1,n)
		{
			ans+=query(n)-query(c[i]);
			insert(c[i]);
		}  
		cout<<ans<<endl;
	}    
}

#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int table[26];
int work[26];
bool FLG = false;
vector<char> V;
map<int,int> MAP;


int main(){

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

		table[i] = 0;
	}

	char S[3][12];
	int len[3];

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

		scanf("%s",S[i]);
		len[i] = 0;
		for(int k = 0; S[i][k] != '\0'; k++){

			table[S[i][k]-'a']++;
			len[i]++;
		}
	}

	int count = 0;

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

		if(table[i] > 0){
			V.push_back('a'+i);
			MAP[i] = count++;
		}
	}
	if(count >= 11){

		printf("UNSOLVABLE\n");
		return 0;
	}

	/*for(int i = 0; i < 3; i++){

		printf("%d\n",len[i]);
	}*/
	//printf("count:%d\n",count);

	ll D[10];
	for(int i = 0; i < 10; i++){
		D[i] = i;
	}

	do{

		ll A = 0;
		bool FLG = true;
		for(int i = 0; i < len[0]; i++){

			if(i == 0 && D[MAP[S[0][i]-'a']] == 0){

				FLG = false;
				break;
			}
			A = 10*A + D[MAP[S[0][i]-'a']];
		}
		if(!FLG ||A == 0)continue;

		ll B = 0;
		for(int i = 0; i < len[1]; i++){
			if(i == 0 && D[MAP[S[1][i]-'a']] == 0){

				FLG = false;
				break;
			}
			B = 10*B + D[MAP[S[1][i]-'a']];
		}
		if(!FLG||B == 0)continue;

		ll C = 0;
		for(int i = 0; i < len[2]; i++){
			if(i == 0 && D[MAP[S[2][i]-'a']] == 0){

				FLG = false;
				break;
			}
			C = 10*C + D[MAP[S[2][i]-'a']];
		}
		if(!FLG||C == 0)continue;

		//printf("A:%lld B:%lld C:%lld\n",A,B,C);

		if(A+B == C){

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

				for(int k = 0; k < len[i]; k++){

					printf("%lld",D[MAP[S[i][k]-'a']]);
				}
				printf("\n");
			}

			return 0;
		}


	}while(next_permutation(D,D+10));


	if(!FLG){

		printf("UNSOLVABLE\n");
	}

	return 0;
}
