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