#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, k, it, t;
long long r[101], rr[101], c[101], cc[101];
long long a[101][101];
long long R, C, x;
vector<long long> ans, ans2;
int main() {
  cin >> n >> m;
  int k = 1;
  x = 3;
  while (n != 0) {
    if (n % 2 == 1) {
      k = k * x % m;
    }
    x = x * x % m;
    n = n / 2;
  }
  cout << (k - 1 + m) % m;
  return 0;
}
