#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define bitsz(x) int(__builtin_popcount(x))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb(x) push_back(x)
#define INF 1e9
#define LINF 1e18
template<class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); };
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int di[4] = {1,0,-1,0};
const int dj[4] = {0,1,0,-1};
const int mod = 1000000007;
//const int mod = 998244353;
//cout << fixed << setprecision(15);



int main() {
    int n; cin >> n;
    ll C; cin >> C;
    map<int,ll> events;

    rep(i,n) {
        int a, b, c;
        cin >> a >> b >> c;
        events[a] += c;
        events[b+1] -= c;
    }

    ll ans = 0;
    ll s = 0;
    int pre = 0;
    for (auto event : events) {
        ans += min(C, s)*(event.first-pre);
        s += event.second;
        pre = event.first;
    }
    cout << ans << endl;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const ll MOD = 1e9 + 7;
const int INF = 1e9;
const ll LLINF = 1e18;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  // input
  vector<string> s(3);
  rep(i, 3) { cin >> s[i]; }

  // solve
  map<char, ll> mp;
  set<char> heads;
  rep(i, 3) {
    reverse(s[i].begin(), s[i].end());
    ll co = i == 2 ? -1 : 1;
    for (char c : s[i]) {
      mp[c] += co;
      co *= 10;
    }
    reverse(s[i].begin(), s[i].end());
    heads.insert(s[i][0]);
  }

  if (10 < mp.size()) {
    cout << "UNSOLVABLE" << endl;
    return 0;
  }

  vector<int> p(10);
  iota(p.begin(), p.end(), 0);
  do {
    int i = 0;
    ll tot = 0;
    for (auto x : mp) {
      char c = x.first;
      ll co = x.second;
      if (p[i] == 0 && heads.count(c)) {
        tot = 1e18;
      }
      tot += co * p[i];
      ++i;
    }
    if (tot == 0) {
      i = 0;
      for (auto& x : mp) {
        x.second = p[i];
        ++i;
      }
      rep(i, 3) {
        ll x = 0;
        for (char c : s[i]) {
          x = x * 10 + mp[c];
        }
        cout << x << endl;
      }
      return 0;
    }
  } while (next_permutation(p.begin(), p.end()));
  cout << "UNSOLVABLE" << endl;
}
