#include <bits/stdc++.h>
using namespace std;
long long n, m, l, r, base, more;
int a[111111];
bool f[111111];
vector<pair<int, long long> > st[111111];
vector<int> pq[111111];
int get(int x, int y) { return (x >> y) & 1; }
void init(long long s) {
  int kt = max(m, n);
  for (int i = 1; i <= kt; i++) a[i] = (n - i + 1) * 2 % base;
  memset(f, true, sizeof(f));
  int t;
  long long tong;
  bool check;
  for (int i = 2; i <= kt; i++)
    if (f[i]) {
      t = i + i;
      while (t <= kt) {
        f[t] = false;
        t += i;
      }
    }
  for (int i = 1; i <= kt; i++) {
    tong = a[i] % base;
    st[i].clear();
    st[i].push_back(make_pair(i, tong));
    t = i + i;
    while (t <= kt) {
      tong = (tong + a[t]) % base;
      st[i].push_back(make_pair(t, tong));
      t += i;
    }
  }
  pq[1].clear();
  for (int i = 2; i <= kt; i++) {
    pq[i].clear();
    if (f[i])
      pq[i].push_back(i);
    else {
      for (int j = 2; j <= kt; j++)
        if (f[j])
          if (i % j == 0) {
            for (int t = 0; t <= int(pq[i / j].size()) - 1; t++)
              pq[i].push_back(pq[i / j][t]);
            check = true;
            for (int t = 0; t <= int(pq[i / j].size()) - 1; t++)
              if (pq[i / j][t] == j) check = false;
            if (check) pq[i].push_back(j);
            break;
          }
    }
  }
}
int found(long long x, long long s) {
  long long low = 0, high = n, mid;
  while (low + 1 < high) {
    mid = (low + high) / 2;
    if (mid * mid + x * x > s)
      high = mid;
    else
      low = mid;
  }
  if (high * high + x * x <= s)
    return high;
  else
    return low;
}
long long binary(long long x, long long fl) {
  int low = 0, high = int(st[x].size()) - 1, mid;
  while (low + 1 < high) {
    mid = (low + high) / 2;
    if (st[x][mid].first <= fl)
      low = mid;
    else
      high = mid;
  }
  if (st[x][high].first <= fl)
    return st[x][high].second;
  else if (st[x][low].first <= fl)
    return st[x][low].second;
  else
    return 0;
}
long long solve(long long s) {
  init(s);
  long long tong = 0, ans = 0, temp, value, ncount;
  int fl;
  for (int i = 1; i <= m; i++) {
    fl = found(i, s);
    temp = 0;
    temp = (temp + binary(1, fl)) % base;
    for (int j = 1; j <= (1 << int(pq[i].size())) - 1; j++) {
      value = 1;
      ncount = 1;
      for (int t = 0; t <= int(pq[i].size()) - 1; t++)
        if (get(j, t)) {
          ncount *= -1;
          value *= pq[i][t];
        }
      temp = (temp + binary(value, fl) * ncount + base) % base;
    }
    tong = (tong + temp) % base;
    ans = (ans + tong) % base;
  }
  if (s >= 1) ans = (ans + more) % base;
  return ans;
}
void khoitao() {
  more = n % base;
  for (int i = 1; i <= m; i++) {
    more = (more + n + 1) % base;
    more = (more + n) % base;
  }
}
int main() {
  cin >> m >> n;
  cin >> l >> r >> base;
  khoitao();
  cout << (solve(r * r) - solve(l * l - 1) + base) % base;
  return 0;
}
