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