#include <bits/stdc++.h>
const double pi = acos(-1.0);
using namespace std;
long long n, m;
long long power(long long a, long long p) {
  long long ret = 1;
  while (p) {
    if (p & 1) ret = ret * a % m;
    a = a * a % m;
    p >>= 1;
  }
  return ret;
}
int main() {
  cin >> n >> m;
  long long ans = power(3ll, n);
  ans = ((ans + m - 1) % m + m) % m;
  cout << ans << endl;
}
