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