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