#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = -1;
  } while (ch < '0' || ch > '9');
  do {
    x = x * 10 + ch - 48;
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return x * f;
}
const int maxn = 1010;
long long n, t, m;
void init() { return; }
int main() {
  n = read();
  m = read();
  if (n == m) {
    puts("0");
    return 0;
  }
  if (m != 0) {
    n--;
    m--;
  }
  int ans = 0;
  m = m * 2;
  if (n >= m) {
    if (n == m)
      ans = m / 2;
    else
      ans = m / 2 + 1;
  } else {
    m /= 2;
    if (n & 1) {
      n = n / 2 + 1;
      m = m - (n - 1);
      ans = n - m;
    } else {
      n = n / 2;
      m -= n;
      ans = n - m;
    }
  }
  printf("%d\n", ans);
  return 0;
}
