戴紅帽的人會在第 M 天看到 M - 1 頂帽子,發現自己也戴紅帽子,因此可出獄。其餘白帽子者 (如果存在) 便會在第 M + 1 天出獄。
Those who wear red would see M - 1 red hats on the M-th day, thus figuring out that she’s wearing red herself, and getting out of jail. Others who wear white (if there’s any) would get out of jail on the M + 1-th day.

ZeroJudge Link (Zh)

#include <stdio.h>

main() {
  int n, m;
  while(scanf("%d %d", &n, &m) != EOF) {
    printf("%d\n", m + ((m==n) ? 0 : 1));
  }
  return 0;
}