#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long binpow(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) res = (res * a) % m;
    a = (a * a) % m;
    n >>= 1;
  }
  return res;
}
int main() {
  cin >> n >> m;
  long long ans = binpow(3, n);
  ans = (ans + m - 1) % m;
  cout << ans;
  return 0;
}
