#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, a, b, i = 0, arr[100000], j = 0;
  cin >> n >> k;
  for (int x = 0; x < n; x++) {
    cin >> a >> b;
    i++;
    while (a != b)
      if (a < b)
        a++, i++;
      else
        a--, i++;
  }
  while (i % k != 0) {
    i++;
    j++;
  }
  cout << j;
  return 0;
}
