#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  while (~scanf("%d%d", &n, &k)) {
    int sum = 0;
    while (n--) {
      int l, r;
      scanf("%d%d", &l, &r);
      sum += r - l + 1;
    }
    printf("%d\n", (k - sum % k) % k);
  }
  return 0;
}
