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