#include <bits/stdc++.h>
//#include <atcoder/all>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long long INF = 1e18;
//const ll mod = 1000000007;
ll num[10];
bool ok = false;

bool f(int a, int b, int c) {
    if((100*a+10*b+c) % 8 != 0) return false;
    bool ret = true;
    num[a]--;
    num[b]--;
    num[c]--;
    for(int i = 1; i <= 9; i++) {
        if(num[i] < 0) ret = false;
    }
    num[a]++;
    num[b]++;
    num[c]++;
    return ret;
}
string S;

void solve() {
    sort(S.begin(), S.end());
    do {
        ll tmp = stoll(S);
        ok |= (tmp % 8 == 0);
    } while(next_permutation(S.begin(), S.end()));
    if(ok) cout << "Yes" << endl;
    else cout << "No" << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> S;
    if(S.size() <= 3) {
        solve();
        return 0;
    }
    for(auto c : S) {
        num[(int)(c - '0')]++;
    }
    for(int i = 1; i <= 9; i++) {
        for(int j = 1; j <= 9; j++) {
            for(int k = 1; k <= 9; k++) {
                ok |= f(i, j, k);
            }
        }
    }
    if(ok) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var) SORT(var); REVERSE(var)
#define OPTIMIZE_STDIO ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed
#define endl '\n'

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

void solve(istream& cin, ostream& cout) {
  int n;
  cin >> n;
  map<int, bool> mp;
  priority_queue<int, vector<int>, greater<int>> q1, q2;
  FOR(i, 0, 200000) {
    q1.push(i);
  }
  REP(i, n) {
    int p;
    cin >> p;
    if (!mp[p]) q2.push(p);
    mp[p] = true;
    while(q1.top() == q2.top()) {
      q1.pop(), q2.pop();
    }
    cout << q1.top() << endl;
  }
}

#ifndef TEST
int main() {
  OPTIMIZE_STDIO;
  solve(cin, cout);
  return 0;
}
#endif
