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