#include <iostream>
#include <string>
#include <math.h>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
#include <stdio.h>
using namespace std;
#define ll long long
int MOD;
ll pow(int base, int exp) {
if(exp == 1) {
return base%MOD;
}
if(exp%2) {
return (base *((pow(base, exp/2) * pow(base, exp/2))%MOD))%MOD;
}
return (pow(base, exp/2) * pow(base, exp/2))%MOD;
}
int main() {
int a, k;
scanf("%d %d",&a,&k);
MOD = a + 1;
if(k == 0)
printf("%d\n", a);
else
printf("%lld\n", pow(a, a));
return 0;
}