#include <bits/stdc++.h>
using namespace std;
long long mpow(long long a, int k, int m) {
  long long res = 1;
  while (k > 0) {
    if (k % 2 == 0)
      a *= a, k /= 2, a %= m;
    else
      res *= a, k--, res %= m;
  }
  return res;
}
int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  long long ans = mpow(3, n, m);
  ans = (ans + m - 1) % m;
  cout << ans << endl;
  return 0;
}
