#include<bits/stdc++.h>
using namespace std;
using ll=int64_t;
template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;}
template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;}

void solve(){
    string s;
    cin>>s;
    if(s.length()==1){
        cout<<(s=="8"?"Yes":"No")<<'\n';
        return;
    }
    if(s.length()==2){
        if(atoi(s.c_str())%8==0){
            cout<<"Yes"<<'\n';
            return;
        }
        swap(s[0],s[1]);
        if(atoi(s.c_str())%8==0){
            cout<<"Yes"<<'\n';
            return;
        }
        cout<<"No"<<'\n';
        return;
    }
    int cnt[10]={};
    for(auto&c:s) ++cnt[c-'0'];
    for(int i{111};i<1000;++i){
        if(i%8) continue;
        int a=i%10,b=(i/10)%10,c=i/100;
        if(!a or !b or !c) continue;
        --cnt[a];--cnt[b];--cnt[c];
        bool f=true;
        for(int j{1};j<=(9);++j){
            if(cnt[j]<0) f=false;
        }
        ++cnt[a];++cnt[b];++cnt[c];
        if(f){
            cout<<"Yes"<<'\n';
            return;
        }
    }
    cout<<"No"<<'\n';
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
}
//#pragma GCC optimize ("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")
//#include <x86intrin.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

#define PI 3.141592653589793L
#define FAST ios::sync_with_stdio(false); cin.tie(NULL);
// Use for file I/O;
#define FIN string _fname = "input"; \
			string _is = _fname + ".in", _os = _fname + ".out"; \
			freopen(_is.c_str(), "r", stdin); \
			freopen(_os.c_str(), "w", stdout);

#define FIN2 freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define RINIT mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define gsize(x) (int)((x).size())

using namespace std;
//using namespace __gnu_pbds;
//typedef gp_hash_table<int, int, hash<int>> ht;

const char nl = '\n';
const char Nl = '\n';

typedef long long ll;
typedef long double ld;
// typedef unsigned int uint;
typedef unsigned long long ull;

const ll inf = 1e9 + 10;
const ll inf2 = 1e18 + 99LL;
const ld inf3 = 1e14;
const ll mod = 1e9+7, mod2 = 998244353;
const ld eps = 1e-2;
const bool local = false;

const int logn = 8, maxn = 1000, maxm = 10001, maxn2 = 3;

int main() {
    FAST; //RINIT;
    
    int n;
    cin >> n;
    string s;
    cin >> s;
    
    if (s[0] != s[n - 1]) {
		cout << 1 << nl;
		return 0;
	}
	
	for (int i = 1; i + 2 < n; i++) {
		if (s[0] != s[i] && s[i + 1] != s[n - 1]) {
			cout << 2 << nl;
			return 0;
		}
	}
	
	cout << -1 << nl;
}


// © Benq 
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/
