#include <bits/stdc++.h>
using namespace std;
const int N = 5e2 + 5;
const long long INF = 1e18 + 1;
const long long Mod = 1e9 + 7;
int n, k, ans, l, r, mn = 1000000;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &l, &r);
    ans += r - l + 1;
  }
  for (int i = 0; i <= 1e5; ++i)
    if ((ans + i) % k == 0) mn = min(mn, i);
  printf("%d", mn);
  return 0;
}
