#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, l, r, i, cnt = 0, nonu = 0;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> l >> r;
    nonu += r - l + 1;
  }
  for (i = 0;; i++) {
    if (nonu % k == 0) {
      break;
    }
    nonu += 1;
    cnt += 1;
  }
  cout << cnt << endl;
  return 0;
}
