#include <bits/stdc++.h>
///#pragma GCC target ("sse4.2")

///#pragma GCC opimize("O1")
///#pragma GCC opimize("O2")

///#pragma GCC opimize("Os")
///#pragma GCC opimize("Ofast")
///#pragma GCC target("avx,avx2,fma")
///#pragma GCC target("avx2")



///#pragma GCC opimize("O3")
///#pragma GCC opimization ("unroll-loops")

using namespace std;

bool home = 1;
typedef long long ll;
typedef long double ld;
#define int ll
signed realMain();


signed main() {
#ifdef ONLINE_JUDGE
  home = 0;
#endif
  if (home) {
    freopen ("tony_stark", "r", stdin);
  } else {
    ios::sync_with_stdio(0); cin.tie(0);
  }
  realMain();
}

const int M = (int) 1e9 + 7;
int n, mod;

signed realMain() {
  cin >> n >> mod;
  if (mod == 0) {
    cout << (n == 1) << "\n";
    return 0;
  }
  int ret = mod - 1, b = n - 1, a = mod - 2;
  while (b) {
    if (b & 1) {
      ret = (ll) ret * a % M;
    }
    b /= 2;
    a = (ll) a * a % M;
  }
  cout << ret << "\n";
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pb push_back
typedef long long ll;
const double pi = acos(-1.0);
#define debug puts("*****************")
#define memset(a,b) memset(a,b,sizeof(a))
const int mod = (1 ? 1000000007 : 998244353);
const int inf = (1 ? 0x3f3f3f3f : 0x7fffffff);
const int dx[] = { -1,0,1,0 }, dy[] = { 0,1,0,-1 };
const ll INF = (1 ? 0x3f3f3f3f3f3f3f3f : 0x7fffffffffffffff);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll poww(ll a, ll b) { ll s = 1; while (b) { if (b & 1)s = (s * a) % mod; a = (a * a) % mod; b >>= 1; }return s % mod; }
/*----------------------------------------------------------------------------------------------------------------------*/

const int N = 1e5 + 11;
ll n,m;

void solve()
{
	cin>>n>>m;
	ll mm=m;
	m--;
	m/=2;
	ll sum=m*2;
	if(mm%2==0&&n<=0)sum++;
	if(n>0&&mm%2==0)
	{
		ll nn=n;
		nn-=(mm/2);
		if(nn>=0)sum++;
	}
	if(n<=0)
	{	
		n*=-1;
		if(m<n&&mm%2==1&&m>0)sum--;
	}
	else if(mm%2==1&&m>0)sum--;
	if(n>0)
	{
		ll q=min(n-1,m);
		sum+=q*2;
		m-=q;
		if(m>0)sum++;
	}
	sum++;
	if(n!=0)sum++;
	cout<<sum<<endl;
}


/*----------------------------------------------------------------------------------------------------------------------*/
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    ll T = 1;
    //cin >> T;
    while (T--)solve();
    return 0;
}
