#include <iostream>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, M; // N: row, M: col
cin >> N >> M;
if (N == 1) cout << 1 << endl;
else if (N == 2) cout << min(4, (M + 1) / 2) << endl;
else {
if (M < 7) cout << min(4, M) << endl;
else cout << M - 2 << endl; // 4번째랑 6번째 col으로만 이동 못함
}
return 0;
}