#include <bits/stdc++.h>
using namespace std;
long long n;
int mod;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("LISFIBO.inp", "r", stdin);
freopen("LISFIBO.out", "w", stdout);
cin >> n >> mod;
if (mod == 1) {
if (n <= 3) {
cout << min(n, 2LL);
} else {
cout << n - 2;
}
} else if (mod == 2) {
cout << (n / 3) * 2 + (n % 3);
} else {
int siz = 8;
int period[siz] = {1, 1, 2, 0, 2, 2, 1, 0};
long long num = n / siz;
int rem = n % siz;
long long res = num * 3;
if (num > 0 && rem <= 2) {
res += 2;
} else {
for (int i = 0, cur = 1; i < rem; ++i) {
res += cur <= period[i];
cur = max(cur, period[i]);
}
}
cout << res;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgogICAgdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiAgICBsb25nIGxvbmcgbjsKICAgIGludCBtb2Q7CgogICAgaW50IG1haW4oKSB7CiAgICAgICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgICAgIGNpbi50aWUoMCk7CiAgICAgICAgY291dC50aWUoMCk7CgogICAgICAgIGZyZW9wZW4oIkxJU0ZJQk8uaW5wIiwgInIiLCBzdGRpbik7CiAgICAgICAgZnJlb3BlbigiTElTRklCTy5vdXQiLCAidyIsIHN0ZG91dCk7CgogICAgICAgIGNpbiA+PiBuID4+IG1vZDsKCiAgICAgICAgaWYgKG1vZCA9PSAxKSB7CiAgICAgICAgICAgIGlmIChuIDw9IDMpIHsKICAgICAgICAgICAgICAgIGNvdXQgPDwgbWluKG4sIDJMTCk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBjb3V0IDw8IG4gLSAyOwogICAgICAgICAgICB9CiAgICAgICAgfSBlbHNlIGlmIChtb2QgPT0gMikgewogICAgICAgICAgICBjb3V0IDw8IChuIC8gMykgKiAyICsgKG4gJSAzKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBpbnQgc2l6ID0gODsKICAgICAgICAgICAgaW50IHBlcmlvZFtzaXpdID0gezEsIDEsIDIsIDAsIDIsIDIsIDEsIDB9OwogICAgICAgICAgICBsb25nIGxvbmcgbnVtID0gbiAvIHNpejsKICAgICAgICAgICAgaW50IHJlbSA9IG4gJSBzaXo7CiAgICAgICAgICAgIGxvbmcgbG9uZyByZXMgPSBudW0gKiAzOwogICAgICAgICAgICBpZiAobnVtID4gMCAmJiByZW0gPD0gMikgewogICAgICAgICAgICAgICAgcmVzICs9IDI7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBmb3IgKGludCBpID0gMCwgY3VyID0gMTsgaSA8IHJlbTsgKytpKSB7CiAgICAgICAgICAgICAgICAgICAgcmVzICs9IGN1ciA8PSBwZXJpb2RbaV07CiAgICAgICAgICAgICAgICAgICAgY3VyID0gbWF4KGN1ciwgcGVyaW9kW2ldKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBjb3V0IDw8IHJlczsKICAgICAgICB9CgogICAgICAgIHJldHVybiAwOwogICAgfQ==