#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define F first
#define S second
#define mod 1000000007
using namespace std;


void solve(){
	ll ans=1e9; int n; cin>>n;
	pii arr[n];
	for(int i=0;i<n;i++){
		int a,b; cin>>a>>b; arr[i]={a,b};
	}
	for(int i=0;i<n;i++){
		ans=min(ans,(ll)arr[i].F+arr[i].S);
		for(int j=i+1;j<n;j++){
			ans=min(ans,(ll)max(arr[i].F,arr[j].S));
			ans=min(ans,(ll)max(arr[i].S,arr[j].F));
		}
	}
	cout << ans <<endl;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int t=1;   ///cin>>t;
    for(int it=1;it<=t;it++){
    	solve();
	}
}




#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
 
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long int           ll;
typedef unsigned long long int  ull;
typedef long double             ld;
 
typedef pair<int, int>          pii;
typedef pair<ll,ll>             pll;
typedef vector<int>             vi;
typedef vector<ll>              vll;
typedef vector<pii>             vpii;
typedef vector<pll>             vpll;
 
#define test(t)             int t; cin >> t; while(t--)
#define f(i, a, b)          for(int (i) = (a); (i) < (b); ++(i))
#define all(a)              (a).begin(), (a).end()
#define endl                "\n"
#define ff                  first
#define ss                  second
#define pb                  push_back
#define deb(x)              cout << #x << ": " << x << endl;
#define deb2(x,y)           cout << #x << ": " << x << " ~ " <<  #y << ": " << y << endl;
#define deba(arr)           cout << #arr << " ~ [ "; for(auto n: arr) cout << n << " "; cout << "]" << endl;
#define debap(arr)          cout << #arr << " ~ [ "; for(auto n: arr) cout << n.first << "-" << n.second << " "; cout << "]" << endl;
#define fast                ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
 
#define mx9        1000000007
#define mx7        10000007
#define mx6        1000006
#define mx5        200005
#define inf        1<<30
#define eps        1e-9
#define mod        1000000007
#define PI         3.141592653589793238462643383279502884L

void solve()
{
    string s;
    cin >> s;
    ll n;
    cin >> n;
    map<ll, ll> mp;
    f(i, 0, n)
    {
        string p, t;
        p = t = s;
        sort(all(t));
        sort(all(p));
        reverse(all(p));
        ll temp = stoi(p) - stoi(t);
        s = to_string(temp);
    }
    cout << s << endl;
}
 
int main()
{
    fast;
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    // test(t)
        solve();
    return 0;
}