#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = (int)1e5 + 10;
const int M = (int)1e4 + 4;
const int INF = (int)0x3f3f3f3f;
const long long MOD = 1e9 + 7;
int coverd, l, r, n, k(0);
int main() {
  std::ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> l >> r;
    coverd += (r - l + 1);
  }
  coverd %= k;
  if (coverd) {
    coverd = k - coverd;
  }
  cout << coverd << "\n";
  return 0;
}
