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