#include<bits/stdc++.h>
#include<algorithm>
#include<string.h>
#include<numeric>
#include<vector>
#include<math.h>
#include<map>
using namespace std;
#define dude             ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mesc(number,character)   char f[number];memset(f,character,number);
#define mesi(number,value)   int f[number];memset(f,value,number);
#define specio           ifstream cin("input.txt");ofstream cout("output.txt");
#define fr(i,start,end)  for(i=start;i<=end;i++)
#define rf(i,start,end)  for(i=start;i>=end;i--)
#define st(vect)       sort(v.begin(),v.end())
#define tst              int t;cin>>t;while(t--)
#define ai               ll n,j,i;cin>>n;ll a[n+1];
#define pi               3.14159265358979323846
#define ll               long long int
#define acc              accumulate
#define ld               long double
#define pb               push_back
#define r0               return 0;
#define pp               pop_back
#define vec              vector
#define sec              second
#define S                string
#define D                double
#define len              length
#define br               break;
#define wh               while
#define fs               first
#define F                float
#define gcd              __gcd
#define sz               size
#define endl              "\n"
#define sp               " "
ll tim(S s) {
    ll val=0;
    val+=(s[0]-48)*600;
    val+=(s[1]-48)*60;
    val+=(s[3]-48)*10;
    val+=(s[4]-48);
    return val;
}
void sagnik(){
ai
ll b[n];
ll  ans=INT_MAX;
fr(i,0,n-1) {
    cin>>a[i]>>b[i];
}
fr(i,0,n-1) {
   fr(j,0,n-1) {
       if(i!=j) {
           ans=min(ans,max(a[i],b[j]));
       }
       else {
           ans=min(ans,(a[i]+b[i]));
       }
   } 
}
cout<<ans<<endl;
}
int main(){
   // dude
   // tst {
   sagnik();
   // }
    r0;
}
#include<bits/stdc++.h>
using namespace std;

const int maxn = 5010;
const int mod = 998244353;

int add (int x, int y) {return x + y >= mod ? x + y - mod : x + y;}
int sub (int x, int y) {return x - y < 0 ? x - y + mod : x - y;}
int mul (int x, int y) {return 1LL * x * y % mod;}
int qpm (int x, int y)
{
    int r = 1;
    while (y)
    {
        if (y & 1) r = mul (r, x);
        x = mul (x, x);
        y >>= 1;
    }
    return r;
}

int n, m;
int s[maxn][maxn], dp[maxn][maxn], ans = 0, _m[maxn];

int main()
{
    scanf ("%d %d", &n, &m);
    _m[0] = 1;
    for (int i = 1; i <= n; i++) _m[i] = mul (_m[i - 1], m);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            int temp = _m[i - 1];
            dp[i][j] = s[i - 1][j];
            s[i][j] = add (mul (s[i - 1][j], m - j), temp);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        int temp = 0;
        for (int j = 1; j <= m; j++)
            temp = add (temp, dp[i][j]);
        ans = add (mul(sub (_m[i], temp), _m[n - i]), ans);
    }
    printf ("%d\n", ans);
	return 0;
}
