#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 15, MOD = 1e9 + 7;
char s[M];
int n, a, b;
long long power(long long x, unsigned int y, int p = 1000) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int main() {
  scanf("%d %d", &a, &b);
  long long te = power(3, a, b);
  printf("%lld", (te + b - 1) % b);
}
