#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
#define fr(i,k,n) for (int i = k; i < n; ++i)
#define fri(i,k,n) for (int i = k; i >= n; --i)
#define pb push_back
#define mp make_pair
#define all(arr) arr.begin(),arr.end()
#define ff first
#define ss second

const double pi=3.1415926535897932384626433832795;
const int inf=1e9;
const ll inf2=1e18;
const int mod=1e9+7;

void boost(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

int n;
vector<vi> a;

bool check(int val){
    vector<bool> mark(32,false),mark2(32,false);
    set<int> ss;
    int num,num2;
    fr(i,0,n){
        num=num2=0;
        fr(j,0,5){
            if(a[i][j]>=val)
                num|=(1<<j);
            else
                num2|=(1<<j);
        }
        if(mark[num2]){
            return true;
        }
        for(auto x: ss){
            num2=x|num;
            if(mark2[num2])
                continue;
            mark2[num2]=true;
            for(int j=num2;;j=(j-1)&num2){
                mark[num2&j]=true;
                if(j==0)
                    break;
            }
        }
        ss.insert(num);
    }
    return false;
}

void solve(){
    cin>>n; 
    a=vector<vi>(n,vi(5,0));
    vi diff;
    fr(i,0,n){
        fr(j,0,5){
            cin>>a[i][j];
            diff.pb(a[i][j]);
        }
    }
    sort(all(diff));
    diff.erase(unique(all(diff)),diff.end());
    int l=0,r=int(diff.size())-1,m;
    int ans=0;
    while(l<=r){
        m=(l+r)/2;
        if(check(diff[m])){
            ans=diff[m];
            l=m+1;
        }
        else{
            r=m-1;
        }
    }
    cout<<ans<<endl;
    return;
}

int main()
{
    boost();
    
    int tc=1;
    //cin>>tc;
    while(tc--)
        solve();
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> p2;
#define sz(a) ll(a.size())
void test2311(){
    string s;
    cin>>s;
    reverse(s.begin(),s.end());
    for(ll i=0;i<sz(s);i++)
        if(s[i]=='6')
            s[i]='9';
        else if(s[i]=='9')
            s[i]='6';
    cout<<s;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    clock_t z=clock();
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll qc=1;
    //cin>>qc;
    for(ll i=1;i<=qc;i++){
        test2311();
    }
    debug("Total Time:%.4Lf\n",(ld)(clock()-z)/CLOCKS_PER_SEC);
}