#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    freopen("digsuper.inp","r",stdin);
    freopen("digsuper.out","w",stdout);
    string n;
    cin >> n;
    long long k;
    cin >> k;
    long long kq=0;
    for(char c: n)
        kq=kq+(c-'0');
    kq=kq*k;
    if(kq%9==0)
    {
        if(kq==0)
            cout << 0;
        else
            cout << 9;
    }
    else
        cout << (kq % 9);
    return 0;

}
