#include <bits/stdc++.h>
using namespace std;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, -1, 1};
const double eps = 1e-9;
const int INF = 0x7FFFFFFF;
const long long INFLL = 0x7FFFFFFFFFFFFFFFLL;
const double pi = acos(-1);
template <class T>
T take(queue<T> &O) {
  T tmp = O.front();
  O.pop();
  return tmp;
}
template <class T>
T take(stack<T> &O) {
  T tmp = O.top();
  O.pop();
  return tmp;
}
template <class T>
T take(priority_queue<T> &O) {
  T tmp = O.top();
  O.pop();
  return tmp;
}
inline void getint(int &num) { scanf("%d", &num); }
inline void getint(long long &num) { cin >> num; }
long long modpow(long long a, long long b, long long c) {
  long long ret = 1LL;
  while (b) {
    if (b & 1) ret = (ret * a) % c;
    a = (a * a) % c;
    b >>= 1;
  }
  --ret;
  ret = (ret + c) % c;
  return ret;
}
int n, m;
int main() {
  cin >> n >> m;
  cout << modpow(3LL, n, m) << endl;
  return 0;
}
