#include<cstdio>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
#define DEBUG(x) cout<<#x<<"="<<x<<endl
#define DEBUG2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl
typedef long long ll;
const int MAXN=2e5+10;
const ll MOD=17;
ll _gcd(ll a,ll b){if(b==0)return a;return _gcd(b,a%b);}
ll gcd(ll a,ll b){a=abs(a),b=abs(b);if(a<b)swap(a,b);return _gcd(a,b);}
ll qpow(ll a,ll n){ll rt=1;while(n){if(n&1)rt=(rt*a)%MOD;a=a*a%MOD;n>>=1;}return rt;}
ll factor[MAXN];
void cal_factor(){factor[0]=1;for(int u=1;u<MAXN;u++){factor[u]=(factor[u-1]*u)%MOD;}}
ll C(ll n,ll k){if(n<k)return 0;return factor[n]*qpow(factor[n-k],MOD-2)%MOD*qpow(factor[k],MOD-2)%MOD;}
void solve(){
    multiset<int>dig;
    string s;
    cin>>s;
    for(char c:s){
        dig.insert(c-'0');
    }
    vector<int>m8[4];
    for(int m=8;m<1000;m+=8){
        int len=0;
        int t=m;
        while(t){
            t/=10;
            len++;
        }
        m8[len].push_back(m);
    }
    if(dig.size()==1){
        if(*dig.begin()==8){
            cout<<"Yes\n";
        }else {
            cout<<"No";
        }
        return;
    }else {
        int f=dig.size()>2?3:2;
        bool match=false;
        for(int m:m8[f]){
            int t=m;
            multiset<int>tdig=dig;
            bool ed=true;
            while(t){
                if(tdig.count(t%10)==0){
                    ed=false;break;
                }else tdig.erase(tdig.find(t%10));
                t/=10;
            }
            if(ed){
                match=true;
                break;
            }
        }
        if(match)cout<<"Yes\n";
        else cout<<"No\n";
    }
}
// #define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
}
#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
//using mint = modint1000000007;
//using mint = modint998244353;
template <typename T> bool chmax(T &u, const T z) { if (u < z) {u = z; return true;} else return false; }
template <typename T> bool chmin(T &u, const T z) { if (u > z) {u = z; return true;} else return false; }
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef pair<ll, int> P;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll a, ll b){return a * b / gcd(a, b);}
//解説写し
int main(){
  int n;
  cin>>n;
  int m=5;
  vector<vector<int>>a(n,vector<int>(m));
  rep(i,n)rep(j,m)cin>>a[i][j];
  int l=0,r=1001001001;
  while(r-l>1){
    int mid=(l+r)/2;
    vector<int>s;
    rep(i,n){
      int x=0;
      rep(j,m){
        if(a[i][j]>=mid)x|=1<<j;
      }
      s.push_back(x);
    }
    sort(s.begin(),s.end());
    s.erase(unique(s.begin(),s.end()),s.end());
    bool ok=false;
    rep(i,s.size())rep(j,i+1)rep(k,j+1){
      if((s[i]|s[j]|s[k])==(1<<m)-1)ok=true;
    }
    if(ok)l=mid;else r=mid;
  }
  cout<<l<<endl;
  return 0;
}