#include <bits/stdc++.h>
using namespace std;
char rd;
int sig;
template <class T>
inline void read(T &a) {
  for (a = 0, rd = getchar(); ('0' > rd || rd > '9') && rd != '-';
       rd = getchar())
    ;
  for (rd == '-' ? sig = -1, rd = getchar() : sig = 1; '0' <= rd && rd <= '9';)
    a *= 10, a += rd - '0', rd = getchar();
  a *= sig;
}
int n, k, l[100005], r[100005];
int main() {
  read(n);
  read(k);
  int tot = 0;
  for (int i = (1); i <= (n); ++i)
    read(l[i]), read(r[i]), tot += r[i] - l[i] + 1;
  int ans = (k - tot % k) % k;
  printf("%d\n", ans);
  return 0;
}
