#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, c{0}, v;
  cin >> n >> k;
  while (n--) {
    int val1, val2;
    cin >> val1 >> val2;
    c += val2 - val1 + 1;
  }
  cout << (k - c % k) % k << endl;
}
