#include <bits/stdc++.h>
using namespace std;
int binmod(int num, int po, int mod) {
  int c = num;
  int ans = 1;
  while (po) {
    if (po & 1) {
      ans = (1LL * ans * c) % mod;
    }
    po >>= 1;
    c = (1LL * c * c) % mod;
  }
  return ans % mod;
}
int main() {
  int n, m;
  long long int cd = 0;
  cin >> n >> m;
  int ans = (cd + binmod(3, n, m) + (m - 1)) % m;
  cout << ans << endl;
}
