#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  long long tot = 0;
  for (int i = 0; i < n; i++) {
    int l, r;
    cin >> l >> r;
    tot += (r - l) + 1;
  }
  if (tot % k == 0) {
    cout << 0 << endl;
  } else {
    long times = floor((double)tot / k);
    long long first = times * k;
    long long last = first + k;
    cout << last - tot << endl;
  }
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
