#include <bits/stdc++.h>
int modExp(long long a, long long n, int p) {
  int ret = 1;
  for (a = (a % p + p) % p; n; n >>= 1, a = 1LL * a * a % p)
    if (n & 1) ret = 1LL * ret * a % p;
  return ret;
}
int main() {
  long long n, m;
  scanf("%lld%lld", &n, &m);
  printf("%lld\n", (modExp(3, n, m) + m - 1) % m);
}
