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