#include <bits/stdc++.h>
using namespace std;
int n, k, l, r, j, i, sum, a[10000000];
int main() {
  cin >> n >> k;
  for (i = 1; i <= n; i++) {
    cin >> l >> r;
    for (j = l + 1000000; j <= r + 1000000; j++)
      if (a[j] == 0) a[j] = 1, sum++;
  };
  cout << (k - sum % k) % k;
  return 0;
}
