#include <bits/stdc++.h>
using namespace std;
int n, m, l, r, p;
long long cal2(long long n, long long m, long long r, long long rn,
               long long rm, long long b, long long f) {
  long long ret = 0;
  for (long long i = 1; i <= n && i * i * b * b <= r; i++) {
    long long l1, r1;
    l1 = 0;
    r1 = m;
    while (l1 < r1) {
      long long mid = (l1 + r1 + 1) / 2;
      if (f) {
        if (i * i * b * b + mid * mid * b * b <= r)
          l1 = mid;
        else
          r1 = mid - 1;
      } else {
        if (i * i * b * b + mid * mid * b * b < r)
          l1 = mid;
        else
          r1 = mid - 1;
      }
    }
    if (l1 == 0) continue;
    ret = (ret + (l1 * (b * (m - l1) % p) % p + (rm + 1) * l1 % p +
                  l1 * (l1 - 1) / 2 % p * b % p) *
                     (n * b + rn + 1 - i * b) % p) %
          p;
  }
  return ret;
}
int pri[100010];
bool vis[100010];
int u[100010];
void get_prime() {
  memset(vis, 0, sizeof(vis));
  u[1] = 1;
  int cnt = 0;
  for (int i = 2; i <= 100000; i++) {
    if (!vis[i]) {
      pri[cnt++] = i;
      u[i] = -1;
    }
    for (int j = 0; j < cnt && pri[j] * i <= 100000; j++) {
      vis[pri[j] * i] = true;
      u[pri[j] * i] = -u[i];
      if (i % pri[j] == 0) {
        u[pri[j] * i] = 0;
        break;
      }
    }
  }
}
long long cal1(long long n, long long m, long long r, long long f) {
  long long ret = 0;
  r = r * r;
  if (r <= 0) return 0;
  if (r == 1 && f == 0) return 0;
  ret = (n * (m + 1) + (n + 1) * m) % p;
  for (long long i = 1; i <= min(n, m); i++) {
    ret += 2 * cal2(n / i, m / i, r, n % i, m % i, i, f) * u[i] % p;
    ret %= p;
  }
  return (ret + p) % p;
}
int main() {
  get_prime();
  cin >> n >> m >> l >> r >> p;
  long long ans = (cal1(n, m, r, 1) - cal1(n, m, l, 0) + p) % p;
  cout << ans << endl;
}
