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