#include <bits/stdc++.h>
namespace Main {
namespace Sourse {}
using namespace Sourse;
static long unsigned int n, k;
static long unsigned int ans;
static inline const void main() {
  scanf("%lu%lu", &n, &k);
  for (register long unsigned int i(0); i < n; ++i) {
    static long signed int l, r;
    scanf("%ld%ld", &l, &r);
    ans = (ans + (r - l + 1)) % k;
  }
  printf("%lu\n", (k - ans) % k);
}
}  // namespace Main
signed int main() {
  Main::main();
  return 0;
}
