#include <bits/stdc++.h>
using namespace std;
inline void wait(double seconds) {
  double endtime = clock() + (seconds * CLOCKS_PER_SEC);
  while (clock() < endtime) {
    ;
  }
}
inline int fastIn() {
  register char c = 0;
  register int a = 0;
  while (c < 33) c = getchar();
  while (c > 33) {
    a = (a * 10) + (c - '0');
    c = getchar();
  }
  return a;
}
long long mod;
inline long long domod(long long x) {
  if (x < 0) x += mod;
  return x % mod;
}
inline long long fastPow(long long a, long long b) {
  long long x = (long long)1, y = domod(a);
  while (b > 0) {
    if (b & 1) {
      x = domod(x * y);
    }
    y = domod(y * y);
    b >>= 1;
  }
  return domod(x);
}
inline void solve() {
  int n;
  cin >> n >> mod;
  long long res = fastPow(3LL, n);
  res = domod(res - 1);
  cout << res << "\n";
}
int main() {
  int kase = 1;
  for (int i = 0, j1 = kase; i < j1; ++i) {
    solve();
  }
  return 0;
}
