#include<bits/stdc++.h> usingnamespacestd; #define forl(i, l, r) for (int i = l; i <= r; i++) #define forr(i, r, l) for (int i = r; i >= l; i--) #define for1(i, n) for (int i = 1; i <= n; i++) #define fro1(i, n) for (int i = 1; i <= n; i++) #define for0(i, n) for (int i = 0; i < n; i++) #define fro0(i, n) for (int i = 0; i < n; i++) #define meminf(a) memset(a, inf, sizeof(a)) #define mem_1(a) memset(a, -1, sizeof(a)) #define mem0(a) memset(a, 0, sizeof(a)) #define memcp(a,b) memcpy(a,b,sizeof(b)) #define oper(type) bool operator <(const type y)const #define mp make_pair #define pu_b push_back #define pu_f push_front #define po_b pop_back #define po_f pop_front #define fi first #define se second #define whiel while #define retrun return typedef pair<longlong, longlong> pll; typedefvector<longlong> vll; typedef pair<int, int> pii; typedefunsignedlonglong ull; typedefvector<int> vii; typedeflongdouble db; typedeflonglong ll; typedefint itn; intin(int &a,int &b,int &c,int &d){returnscanf("%d%d%d%d",&a,&b,&c,&d);} intin(int &a,int &b,int &c){returnscanf("%d%d%d",&a,&b,&c);} intin(int &a,int &b){returnscanf("%d%d",&a,&b);} intin(ll &a){returnscanf("%lld",&a);} intin(int &a){returnscanf("%d",&a);} intin(char *s){returnscanf("%s",s);} intin(char &s){returnscanf("%c",&s);} intin(db &a){returnscanf("%Lf",&a);} voidout(int a){printf("%d ",a);} voidoutln(int a){printf("%d\n",a);} voidout(ll a){printf("%lld ",a);} voidoutln(ll a){printf("%lld\n",a);} const db pi = acos((db)-1); const ll inf =0x3f3f3f3f; const db eps = 1e-8; constint N = 2.1e5; const ll mod = 1e9+7; intsign(db a){ return a < -eps ? -1 : a > eps;} intdb_cmp(db a, db b){ return sign(a-b); }
ll qPow(ll a, ll b, ll c){ //求(a^b) % c ll ret = 1; while (b) { if (b & 0x1) ret = ret * a % c; a = a * a % c; b >>= 1; } return ret; } intmain(){ int n,a; whiel(~in(n,a)){ int ta=a,k=0,maxn=1<<n; whiel(ta%2==0){ k++; ta/=2; } if(k&&n){ itn kk=n/a; if(n%a)kk++; int ans=0; int low=n/k; if(n%k==0)low--; for1(i,low){ if(qPow(a,i,maxn)==qPow(i,a,maxn))ans++; } kk=1<<kk; ans+=(1<<n)/kk-low/kk; outln(ans); }else { puts("1"); } } return0; }