#include <bits/stdc++.h>
using namespace std;
long long n, mod;
long long expsq(long long base, long long exp) {
  if (exp == 0) return 1ll;
  if (exp == 1) return base;
  if (exp & 1)
    return (base * expsq((base % mod * base % mod) % mod, (exp - 1) / 2)) % mod;
  return expsq((base % mod * base % mod) % mod, exp / 2) % mod;
}
int main() {
  cin >> n >> mod;
  long long ans = expsq(3ll, n);
  ans--;
  while (ans < 0) ans += mod;
  cout << ans % mod;
}
