#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <functional>
#include <time.h>
bool check(std::string& t);

int main() {
    clock_t  begin=clock();
    std::map<char,int> g;
    std::string d("sendmory");
    for(auto c: d)
        g[c] = 0;
    for(int i = 10000000; i <= 99999999; i++){
        std::string temp = std::to_string(i);
        if(check(temp)){
            int j=0;
            for(auto a: d){
                g[a] = temp[j] -'0';
                j++;
            }
            if(g[d[4]] && g[d[0]]){
                int sl1,sl2,sl3;
                sl1 = g[d[0]]*1000 + g[d[1]]*100 + g[d[2]]*10 + g[d[3]];
                sl2 = g[d[4]]*1000 + g[d[5]]*100 + g[d[6]]*10 + g[d[1]];
                sl3 = g[d[4]]*10000 + g[d[5]]*1000 + g[d[2]]*100 + g[d[1]]*10 + g[d[7]];
                if(sl1+sl2 == sl3) {
                    for (auto c:g)
                        std::cout << c.first << " = " << c.second << "\n";
                    std::cout << sl3 <<"\n";
                    break;
                }

            }

        }

    }
    clock_t end=clock();
    std::cout << end - begin;
    return 0;
}

bool check(std::string& t){
    for(int i=0; i < t.size();i++){
        for(int j = i+1; j <t.size();j++)
            if(t[i]==t[j])
                return false;
    }
    return true;
}