#include <bits/stdc++.h>
using namespace std;
int N = 0, K = 0, moves = 0, l, r, value = 0;
int main() {
  scanf("%d%d", &N, &K);
  for (int i = 0; i < N; i++) {
    scanf("%d %d\n", &l, &r);
    value += r - l + 1;
  }
  while (value % K != 0) {
    value++;
    moves++;
  }
  cout << moves << '\n';
  return 0;
}
