#include <bits/stdc++.h>
using namespace std;
int N;
int main() {
  int T, cas = 0;
  int i, j, k, l, r;
  scanf("%d%d", &N, &k);
  int sum = 0;
  for (i = 0; i < N; i++) {
    scanf("%d%d", &l, &r);
    sum += (r - l + 1);
  }
  sum %= k;
  sum = k - sum;
  sum %= k;
  cout << sum << endl;
  return 0;
}
