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