#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, k;
  cin >> n >> k;
  long long int counter = 0;
  for (long long int i = 0; i < n; i++) {
    long long int l, r;
    cin >> l >> r;
    counter = (counter + r - l + 1) % k;
  }
  cout << (k - counter) % k;
}
