#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  long long a, b, ans = 0;
  cin >> a >> b;
  vector<long long> x(a);
  for (int i = 0; i < a; i++) {
    x[i] = 1;
  }
  int i = 0;
  while (b > 0) {
    if (i > a - 1)
      i = i - a;
    else
      ;
    if (x[i] != 0) {
      x[i] = 0;
      i = i + 2;
      b--;
    } else
      i++;
  }
  for (int i = 0; i < a; i++) {
    if (x[i + 1] == 0 && x[i] == 1)
      ans++;
    else if (i == a - 1 && x[a - 1] == 1)
      ans++;
    else
      ;
  }
  cout << ans << endl;
  return 0;
}
