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