#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  long long x = 0;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    int l, r;
    cin >> l >> r;
    x += r - l + 1;
  }
  int mod = x % k;
  cout << (k - mod) % k << endl;
  return 0;
}
