#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, n, k, c = 0;
  cin >> n >> k;
  while (n--) {
    cin >> a >> b;
    c += b - a + 1;
  }
  c = c % k;
  cout << (k - c) % k;
}
