#include<stdio.h>
int main(void)
{
  int N,K,min,m;
  scanf("%d%d", &N, &K);
  min = N - K;
  m = min;
  if(m < 0)
    m = m*-1;
  while(1)
  {
    if(min < 0)
    {
      min = min*-1;
      if(min > m)
        break;
    }
    if(min >= N)
    {
      m = N;
      break;
    }
    min = min - K;
    m = min;
  }
  printf("%d\n", m);
  
  return 0;
}