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