#include <bits/stdc++.h>
using namespace std;
bool dbg = 0;
int main() {
  int N, K;
  cin >> N >> K;
  long long Count = 0;
  int x, y;
  for (int n = 0; n < N; ++n) {
    scanf("%d%d", &x, &y);
    Count += (long long)(y - x + 1);
  }
  cout << (K - Count % K) % K << endl;
  return 0;
}
