#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  int m;
  cin >> n >> k;
  int covered = 0;
  for (int i = 0; i < n; i++) {
    int start, end;
    cin >> start >> end;
    covered += end - start + 1;
  }
  if (covered != k && covered % k != 0) {
    m = (k - (covered % k));
    cout << m;
  } else {
    m = 0;
    cout << m;
  }
}
