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