'use strict';

const inputs = require('fs').readFileSync('/dev/stdin', 'utf8').split('\n');

const input = inputs[0].split(' ').map(v => parseInt(v, 10));

if (input[0] % input[1] === 0) {
  console.log(0);
  return;
}

for (;;) {
  const result = Math.abs(input[0] - input[1]);

  if (result >= input[0]) {
    break;
  }

  input[0] = result;
}

console.log(input[0]);