#include <bits/stdc++.h>
int n, m;
int power(int a, int n) {
  int res = 1;
  while (n) {
    if (n & 1) res = (1ll * res * a) % m;
    a = (1ll * a * a) % m;
    n = n / 2;
  }
  return res;
}
int main() {
  scanf("%d %d", &n, &m);
  printf("%d", (power(3, n) - 1 + m) % m);
  return 0;
}
