#include <bits/stdc++.h>
#ifdef LILY
#include "Debug.h"
#else
#define var(...) (0)
#define dbg(...) (0)
#endif
using int32 = int;
using int64 = long long;
using namespace std;

class Solution
{
#define int int64
#define sfor(i, n) for (int i = 1; i <= (n); ++i)
#define tfor(i, n) for (int i = 0; i < (n); ++i)
#define INF 0x3f3f3f3f
#define EPS 1e-5
#define M 1000000007

    int n;
    int p;

    int pow(int base, int ex)
    {
        int ret = 1;
        while (ex)
        {
            if (ex & 1) ret = ret * base % M;
            ex >>= 1;
            base = base * base % M;
        }
        return ret;
    }

    void reals()
    {
    }

public:
    Solution()
    {
        cin >> n >> p;
        int t = pow(p - 2, n - 1);
        cout << t * (p - 1) % M << endl;
    }
#undef int
#undef tfor
#undef sfor
};

int32 main()
{
#ifdef LILY
    while (1)
#endif
        Solution();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}

vector< int > divisor(int n) {
  vector< int > ret;
  for(int i = 1; i * i <= n; i++) {
    if(n % i == 0) {
      ret.push_back(i);
      if(i * i != n) ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  reverse(begin(ret), end(ret));
  return (ret);
}

signed main(){
    int A; cin>>A;
    int B; cin>>B;
    int ans = 1;

    for(int i=A;i<=B;i++){
        vec_int temp = divisor(i);
        for(auto num: temp){
            if(i+num<=B){
                ans = max(num, ans);
                break;
            }
        }
    }
    cout<<ans<<endl;

    return 0;
}