#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, k, sum = 0, moves = 0;
  int l[100000] = {0}, r[100000] = {0};
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i];
    sum += r[i] - l[i] + 1;
  }
  if (sum % k == 0)
    cout << moves;
  else if (sum % k != 0 && sum > k) {
    moves = k - (sum % k);
    cout << moves;
  } else if (sum % k != 0 && sum < k) {
    moves = k - sum;
    cout << moves;
  }
  return 0;
}
