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