#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50, MOD = 1e9 + 7;
long long kol[N], a[N];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  if (n == m) {
    cout << 0;
    return 0;
  }
  if (m == 0 || m == 1) {
    cout << 1;
    return 0;
  }
  long long kol = 0;
  if (n / 2 >= m) {
    cout << m;
    return 0;
  } else {
    m = (n + 1) / 2 - (m - n / 2);
    cout << m;
    return 0;
  }
}
