#include <bits/stdc++.h>
using namespace std;
string FILE_NAME = "testcase.C";
string NAME;
string itos(int n) {
  stringstream ss;
  ss << n;
  return ss.str();
}
long long mod_pow(long long x, long long n, long long mod) {
  long long res = 1LL;
  while (n > 0) {
    if (n & 1) res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  long long res = mod_pow(3LL, (long long)n, (long long)m);
  res = (res - 1LL + (long long)m) % m;
  cout << res << endl;
  return 0;
}
