#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define N 100007
#define mod 19260817
#define mid ((l + r) >> 1)
using namespace std;
typedef long long ll;

inline ll rd() {
    ll x = 0;
    bool f = 0;
    char c = getchar();
    for(; !isdigit(c); c = getchar()) 
        if (c == '-') f = 1;
    for(; isdigit(c); c = getchar())
        x = x * 10 + (c ^ 48);
    return f ? -x : x;
}

inline ll fpow(ll x, ll t, ll p) {
  ll res = 1;
  x = x % p;
  while (t) {
    if (t & 1) res = 1ll * res * x % p;
    x = 1ll * x * x % p; t >>= 1;
  }
  return res;
}

int main() {
    int a = rd();
    int b = rd();
    int c = rd();
    int t;
    if (b > 1 && c >= 2) t = fpow(b, c, 4) + 4;
    else t = fpow(b, c, 10000000);
    printf("%lld\n", fpow(a, t, 10));
    return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<math.h>
#include<iostream>
#include<cmath>
#include<queue>
#include<vector>
#define INF 0x7fffffff
#define IOS ios::sync_with_stdio(false);cin.tie();cout.tie(0)

using namespace std;
typedef long long ll;

const int N=1e5+100;
//const int mod=1e9+7;
string str;
bool st[N];

int main()
{
    IOS;
    int n;
    cin>>n;
    cin>>str;
    if(n==1) {cout<<"-1"<<endl;return 0;}
    if(str[0]!=str[n-1])
    {
        cout<<"1"<<endl;
    }
    else
    {
        int w1=0;
        for(int i=0;i<n;i++)
        {
            if(str[i]!=str[0])
                st[i]=true;
        }
        int flag=0;
        for(int i=1;i<n;i++)
            if(st[i-1]&&st[i]) flag=1;
        if(!flag) cout<<"-1"<<endl;
        else cout<<"2"<<endl;
    }
    return 0;
}
