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