#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define endl "\n"
#define f first
#define s second
#define ar array
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define sz(X) ((int)(X).size())
#define pcnt __builtin_popcount
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define get_pos(c, x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define ms(c, x)  memset(c,x,sizeof c)
#define  No(x) cout<<(x?"NO":"No")<<endl;
#define  Yes(x) cout<<(x?"YES":"Yes")<<endl;
#define  nl cout<<endl;
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = l; i <=r; i++)
#define ford(i, n) for (int i = n - 1; i >= 0; --i)
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;

using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;

template<typename T>
using osl = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using osg = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;

// for greater use greater<T>
template<typename T>
ostream &operator+(ostream &out, const vector<T> &vec) {
    for (const auto &x : vec) {
        out << x << " ";
    }
    out << "\n";
    return out;
}

template<typename T>
ostream &operator*(ostream &out, const vector<T> &vec) {
    for (const auto &x : vec) {
        out + x;
    }
    return out;
}

template<typename T>
istream &operator>>(istream &in, vector<T> &vec) {
    for (auto &x : vec) {
        in >> x;
    }
    return in;
}

template<class T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }

template<class T>
bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

#ifdef LOCAL

void debug() { cerr << endl; }

template<class T, class... Args>
void debug(const T &t, const Args &... args) {
    cerr << t << " ";
    debug(args...);
}

#define dbg(...) {cerr<<__LINE__<<" [[DEBUG]] ";debug(__VA_ARGS__);};
#else
#define dbg(...) void(0)
#endif
const ll mod = 1e9 + 7;
//const int mod = 998244353;
const ll INF = 1e17 + 6;

inline ll power(ll x, ll y) {
    ll res = 1;
    x = x;
    while (y > 0) {
        if (y & 1)
            res = (res * x);
        y = y >> 1;
        x = (x * x);
    }
    return res;
}

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
const int MXN=3e6+5;

void solve() {
   int n,m;
   cin>>n>>m;
   vl h(n);
   cin>>h;
   vl w(m);
   cin>>w;
   vi pref((n+1)/2+2);
   vi suff((n+1)/2+2);
   sort(all(h));
   for(int i=0;i<n-1;i+=2) {
       pref[i / 2+1] += pref[i / 2] + h[i + 1] - h[i];
   }
   for(int i=n-2;i>0;i-=2) {
       suff[i / 2] += suff[i / 2+1] + h[i+1] - h[i];
   }
   ll ans=INF;
   for(auto i:w){
       int pos=get_pos(h,i);
       if(pos&1){
           pos--;
       }
       ans=min(pref[pos/2]+suff[pos/2]+abs(i-h[pos]),ans);
   }
   cout<<ans<<endl;
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(10);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t = 1, tc = 1;
//    cin >> t;
    while (t--) {
        // cout<<"Case #"<<tc<<": " ;
        solve();
        tc++;
    }
#ifdef LOCAL
    cerr << endl << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
#endif
    return 0;
}
//look if it requires ll
#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << "Line " << __LINE__ << ": " <<  #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()

using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;

template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;

template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};

const ll INF = (1ll<<62);
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;

int main(){

    V<string> S(3);
    REP(i, 3) cin >> S[i];

    V<char> C;
    REP(i, 3){
        REP(j, S[i].size()){
            C.push_back(S[i][j]);
        }
    }

    UNIQUE(C);
    // dump(C)

    if((ll)C.size() > 10){
        cout << "UNSOLVABLE" << endl;
        return 0;
    }
    ll sz = C.size();
    V<ll> c2num(26, -1);
    REP(i, sz) c2num[C[i] - 'a'] = i;

    // dump(sz)

    V<ll> used(10, -1);
    V<ll> vals(sz);
    ll cnt = 0;
    auto dfs = [&](auto self, ll now) -> void{
        if(now == sz){
            cnt++;
            // dump(vals)

            if(vals[c2num[S[0][0]-'a']] != 0 and vals[c2num[S[1][0]-'a']] != 0 and vals[c2num[S[2][0]-'a']] != 0){
                V<ll> X(3, 0);
                REP(i, 3){
                    REP(j, S[i].size()){
                        X[i] *= 10;
                        ll v = vals[c2num[S[i][j]-'a']];
                        X[i] += v;
                    }
                }

                if(X[0] + X[1] == X[2]){
                    REP(i, 3){
                        cout << X[i] << endl;
                    }
                    exit(0);
                }
            }

        }else{
            REP(i, 10){
                if(used[i] == -1){
                    used[i] = 1;
                    vals[now] = i;
                    self(self, now+1);
                    used[i] = -1;
                    vals[now] = -1;
                }
            }
        }
        return;
    };

    dfs(dfs, 0);

    dump(cnt)

    cout << "UNSOLVABLE" << endl;

    return 0;
}