#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long q_pow(long long a, long long b, long long c) {
  if (b == 0) return 1;
  long long ret = q_pow(a, b / 2, c);
  ret = (ret * ret) % c;
  if (b & 1) ret = (ret * a) % c;
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  long long ans = (q_pow(3, n, m) + m - 1) % m;
  cout << ans << endl;
  return 0;
}
