#include <bits/stdc++.h>
using namespace std;
int n, mod;
int bin(int a, int b) {
  int ret = 1;
  while (b > 0) {
    if (b & 1) {
      ret = 1LL * ret * a % mod;
    }
    b >>= 1;
    a = 1LL * a * a % mod;
  }
  return ret;
}
int main() {
  cin >> n >> mod;
  cout << (bin(3, n) + mod - 1) % mod;
  return 0;
}
