#include <bits/stdc++.h>
using namespace std;
int n;
long long mod;
long long exp_fast(long long a, int n) {
  if (!n) return 1LL;
  long long res = exp_fast(a, n / 2);
  res = (res * res) % mod;
  if (n % 2) res = (res * a) % mod;
  return res;
}
int main() {
  cin >> n >> mod;
  long long res;
  res = exp_fast(3LL, n);
  res = (res - 1 + mod) % mod;
  cout << res << endl;
}
