#include <bits/stdc++.h>
using namespace std;
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  int n, k, start = 0, coun = 0, en = 0;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> start >> en;
    coun += max(en, start) - min(start, en) + 1;
  }
  if (coun % k == 0)
    cout << "0";
  else
    cout << (k - (coun % k));
  return 0;
}
