#include <bits/stdc++.h>
using namespace std;
const int N = 200005, M = 1005;
const long long MOD = 1e9 + 7;
const double eps = 1e-6;
const double PI = acos(-1.0);
int n, m;
int fast_pow(int b, int n) {
  if (n == 1) return b;
  if (n == 0) return b;
  if (n % 2 == 1) return (1ll * b * fast_pow(b, n - 1)) % m;
  int r = fast_pow(b, n / 2);
  return (1ll * r * r) % m;
}
int main() {
  cin >> n >> m;
  int ans = (fast_pow(3, n) - 1 + m) % m;
  printf("%d\n", ans);
  return 0;
}
