#include <bits/stdc++.h>
using namespace std;
int mod_pow(long long x, int n, int mod) {
  long long s = 1;
  while (n) {
    if (n & 1) s = s * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return (int)s;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int ans = mod_pow(3, n, m);
  printf("%d\n", (ans + m - 1) % m);
  return 0;
}
