#include <bits/stdc++.h>

using namespace std;

vector<int> results;
string res, a, b, c;
vector<bool> used(10, 0);

bool found = 0;

long long getNum(string a)
{
    long long num = 0;
    for(int i = 0; i < a.size(); ++i)
    {
        // find where it is in res;
        num = num * 10 + results[res.find(a[i])];
    }
    return num;
}

void dp(int depth)
{
    if(found) return;
    else if(depth == results.size())
    {
        // try and check
        long long d = getNum(a), e = getNum(b), f = getNum(c);
        if(d + e == f && d > 0 && e > 0 && f > 0)
        {
            found = 1;
            cout << d << endl << e << endl << f << endl;
        }
        return;
    }
    for(int i = 0; i < 10; ++i)
    {

        if(!used[i] && (i != 0 || (res[depth] != a[0] && res[depth] != b[0]  && res[depth] != c[0])))
        {
            results[depth] = i;
            used[i] = 1;
            dp(depth + 1);
            used[i] = 0;
        }
    }
}

int main()
{
    cin >> a >> b >> c;
    res = a + b + c;
    sort(res.begin(), res.end());
    res.resize(distance(res.begin(), unique(res.begin(), res.end())));
    if(res.size() > 10)
    {
        cout << "UNSOLVABLE" << endl;
        return 0;
    }
    results = vector<int>(res.size());
    // dp results
    dp(0);
    if(!found)
        cout << "UNSOLVABLE" << endl;
    return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long long n,m,q,usx=0,usy=200001;
long long getint()
{
	char ch;do	{ch=getchar();}while (ch!='-'&&(ch<'0'||ch>'9'));
	long long ans=0,f=0;if (ch=='-') f=1; else ans=ch-'0';
	while (isdigit(ch=getchar())) ans=ans*10+ch-'0';
	if (f) ans*=-1;
	return ans;
}
long long tr[200001];
bool vis[200001];
int lowbit(int x)
{
	return x&-x;
}
void add(int x)
{
	while(x<=n)
	{
		tr[x]+=1;
		x+=lowbit(x);
	}
	return ;
}
long long query(int x)
{
	long long ans=0;
	while(x)
	{
		ans+=tr[x];
		x-=lowbit(x);
	}
	return ans;
}
struct Q
{
	int x,y;
	void in()
	{
		x=n-getint()+1;
		y=getint();
		if(x==n)usy=usy<y? usy:y;
		if(y==1)usx=usx>x? usx:x;
	}
}nod[600011];
bool cmp(const Q& x,const Q& y)
{
	return x.y==y.y? x.x<y.x:x.y<y.y;
}
int main()
{
	n=getint();m=getint();q=getint();
	for(int i=1;i<=q;i++)
	  nod[i].in();
	for(int i=1;i<usx;i++)
	{
		nod[++q].x=i;
		nod[q].y=1;
	}
	for(int i=usy+1;i<=m;i++)
	{
		nod[++q].x=n;
		nod[q].y=i;
	}
	sort(nod+1,nod+q+1,cmp);
	long long ans=0;
	for(int i=1;i<=q;i++)
	{
		if(!vis[nod[i].x])
		{
			vis[nod[i].x]=true;
			add(nod[i].x);
		}
		if(nod[i].y!=nod[i+1].y)
		  ans+=query(nod[i].x);
	}
	cout<<n*m-ans;
 } 