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