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