#include "bits/stdc++.h"
#define int long long
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 1000000007;


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	string s1, s2, s3;
	cin >> s1 >> s2 >> s3;
	reverse(s1.begin(), s1.end());
	reverse(s2.begin(), s2.end());
	reverse(s3.begin(), s3.end());
	int n1 = s1.size(), n2 = s2.size(), n3 = s3.size();
	map<char, int>mp;
	for (int i = 0; i < n1; i++) {
		mp[s1[i]]++;
	}
	for (int i = 0; i < n2; i++) {
		mp[s2[i]]++;
	}for (int i = 0; i < n3; i++) {
		mp[s3[i]]++;
	}
	map<char, int>tmp;
	int now = 0;
	for (auto p : mp) {
		tmp[p.first] = now;
		now++;
	}
	mp = tmp;
	int S = mp.size();
	if (S > 10) {
		cout << "UNSOLVABLE" << endl; return 0;
	}
	vector<int>pe;
	for (int i = 0; i < 10; i++) {
		pe.push_back(i);
	}
	do {
		bool ok = true;
		int t = 1;
		int ans1 = 0, ans2 = 0, ans3 = 0;
		for (int i = 0; i < n1; i++) {
			int nv = pe[mp[s1[i]]];
			if (i == n1 - 1 && nv == 0)ok = false;
			ans1 += nv * t;
			t *= 10;
		}
		t = 1;
		for (int i = 0; i < n2; i++) {
			int nv = pe[mp[s2[i]]];
			if (i == n2 - 1 && nv == 0)ok = false;
			ans2 += nv * t;
			t *= 10;
		}
		t = 1;
		for (int i = 0; i < n3; i++) {
			int nv = pe[mp[s3[i]]];
			if (i == n3 - 1 && nv == 0)ok = false;
			ans3 += nv * t;
			t *= 10;
		}
		if (ans1 + ans2 != ans3)ok = false;
		if (ok) {
			cout << ans1 << endl;
			cout << ans2 << endl;
			cout << ans3 << endl;
			return 0;
		}
	} while (next_permutation(pe.begin(), pe.end()));
	cout << "UNSOLVABLE" << endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define per(i, a, b) for(int i = (b)-1; i >= (a); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair

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; }

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pii> vii;

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

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n, k;
  cin >> n >> k;

  vi cnt(n+1);
  rep(i,0,n) {
    int x;
    cin >> x;
    ++cnt[x];
  }

  int tot = min(cnt[0], k);
  ll ans = 0;
  rep(i,1,n+1) {
    if (tot == 0) break;
    // how many i do we have
    int nxt = min(tot, cnt[i]);
    int rm = tot-nxt;
    ans += ll(i) * ll(rm);
    tot = nxt;
  }
  cout << ans << '\n';
}