#include <bits/stdc++.h>
using namespace std;
int mod;
long long int power_int(int a, int b) {
  long long int x = 1, y = a;
  while (b > 0) {
    if (b & 1) x = ((x * y) + mod) % mod;
    y = ((y * y) + mod) % mod;
    b >>= 1;
  }
  return x;
}
int main() {
  int n;
  cin >> n >> mod;
  long long int k = (power_int(3, n) + mod) % mod;
  k = ((k - 1) + mod) % mod;
  cout << k << endl;
  return 0;
}
