import java.util.*; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int n = sc.nextInt(); int k = sc.nextInt(); if (k==0 || k>n) {System.out.println(0);} else if (n%2==0) {System.out.println(n-2);} else { if (k==n/2+1) {System.out.println(n-1);} else {System.out.println(n-2);} } } }