#include <bits/stdc++.h>
const int MAXN = 1e9 + 7;
const int INF = 1e9 + 7;
using namespace std;
bool comp1(pair<signed long long, signed long long> a,
           pair<signed long long, signed long long> b) {
  if (a.second == b.second) return a.first < b.first;
  return (a.second < b.second);
}
const signed long long M = 998244353;
signed long long modpow(signed long long x, signed long long n,
                        signed long long M) {
  signed long long result = 1;
  while (n) {
    if (n % 2 == 1) result = ((result % M) * (x % M)) % M;
    x = ((x % M) * (x % M)) % M;
    n = n / 2;
  }
  return result;
}
signed long long inv(signed long long A, signed long long M) {
  return modpow(A, M - 2, M);
}
const signed long long N = 10e6 + 6;
signed long long cnt[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  signed long long n, m;
  cin >> n >> m;
  if (n == m) {
    cout << 0;
    return 0;
  }
  if (m == 0) {
    cout << 1 << "\n";
    return 0;
  }
  if (m * 2 <= n)
    cout << m << "\n";
  else {
    cout << m - (m * 2 - n) << "\n";
  }
  return 0;
}
