#include <bits/stdc++.h>
using namespace std;
string FILE_NAME = "testcase.289A";
string NAME;
string itos(int n) {
  stringstream ss;
  ss << n;
  return ss.str();
}
int main() {
  ios_base::sync_with_stdio(0);
  long long n, k;
  cin >> n >> k;
  long long p = 0LL;
  for (int i = 0; i < (n); i++) {
    int l, r;
    cin >> l >> r;
    p += (long long)(r - l + 1LL);
  }
  if (p % k == 0LL) {
    cout << 0 << endl;
  } else {
    long long res = (k - (p % k) + k) % k;
    cout << res << endl;
  }
  return 0;
}
