#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int N = 1e5 + 7, M = 1e7, OO = 0x3f3f3f3f;
int main() {
  int n, k, start, endo, sum = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> start >> endo;
    sum += (endo - start) + 1;
  }
  sum %= k;
  if (sum != 0) {
    sum = k - sum;
  }
  cout << sum;
  return 0;
}
