#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
int N,M;
vector<pii>adj[2005];
int taka[2005];
bool vis[2005];
int bfs(int s)
{
    fill(vis+ 1, vis + N + 1, false);
    fill(taka + 1, taka + N + 1, 0);
    priority_queue<pii, vector<pii>, greater<pii> >q;
    q.push({0,s});//node taka;
    while(!q.empty())
    {
        pii node=q.top();//node taka..
        q.pop();
        if((node.second==s) and vis[s])
        {
            cout<<node.first<<endl;
            return 0;
        }
        if(vis[node.second])continue;
        vis[node.second]=true;
        taka[node.second]=node.first;
        for(pii x:adj[node.second])
        {
            if((vis[x.second]==true) and (x.second!=s))
                continue;
            q.push({(x.first+node.first),x.second});
        }
    }
    cout<<-1<<endl;
    return 0;
}
int main()
{
    cin>>N>>M;
    for(int i=0; i<M; i++)
    {
        int u,v,c;
        cin>>u>>v>>c;
        adj[u].push_back({c,v});
    }
    for(int i=1; i<=N; i++)
        bfs(i);

    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define sz 1600008
#define mod 998244353
#define f first
#define s second
ll arr[sz+10],brr[sz+10];
ll num=1;
char c[sz];
std::vector<ll> v[34];
long long binpow(long long a, long long b,long long mod1) {
    a %= mod1;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % mod1;
        a = a * a % mod1;
        b >>= 1;
    }
    return res;
}
int main()
{
  int t=1;
  //scanf("%d",&t);
  for(int cs=1;cs<=t;cs++)
  {
      ll a,b,c;
      scanf("%lld %lld %lld",&a,&b,&c);
      a%=10;
      ll n=a;
      arr[n]++;
      ll val=0;
      for(int i=2;;i++)
      {
       n=(n*a)%10;
       if(arr[n])
       {
        val=i-1;
        break;
       }
      }
      ll a1=binpow(b,c,val);
      if(a1==0)
      	a1=val;
      	n=a;
      	for(int i=2;i<=a1;i++)
      		n=(n*a)%10;
        a=n;
        printf("%lld\n",a);
  }
  return 0;
}