#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const long long INF = LONG_MAX;
int n, m;
long long binpow(long long a, long long n, long long m) {
  if (!n)
    return 1 % m;
  else if (n & 1)
    return (binpow(a, n - 1, m) * a) % m;
  else {
    long long t = binpow(a, n / 2, m);
    return (t * t) % m;
  }
}
int main() {
  cin >> n >> m;
  long long s = binpow(3, n, m);
  s--;
  if (s < 0) s += m;
  cout << s;
  return 0;
}
