#include<stdio.h>
#define min(a,b) ((a)<(b))?a:b
int main()
{

	int l=0,r=0,u=0,d=0;
	char c;
	int i,n;
	scanf("%d \n",&n);

	for(i=0;i<n;i++)
	{

	   scanf("%c",&c);
	   switch (c)
	   {

	   case  'U' :u++;break;
	   case  'D' :d++;break;
	   case  'L' :l++;break;
	   case  'R' :r++;break;
       }

    }
    r=min(l,r);
    d=min(u,d);
    //printf("l:%d r:%d u:%d d��%d\n",l,r,u,d) ;
    printf("%d",2*(r+d));
	return 0;
 }

#include <stdio.h>
#define MAXN 100

int main()
{
    int n, cnt[4] = {0};
    char seq[MAXN];
    enum { U, D, L, R };
    scanf("%d %s", &n, seq);
    for (int i = 0; i < n; i++) {
        if (seq[i] == 'U') cnt[U]++;
        else if (seq[i] == 'D') cnt[D]++;
        else if (seq[i] == 'L') cnt[L]++;
        else cnt[R]++;
    }
    int min_ud = ((cnt[U] < cnt[D])? cnt[U]: cnt[D]) * 2;
    int min_lr = ((cnt[L] < cnt[R])? cnt[L]: cnt[R]) * 2;
    printf("%d\n", min_ud + min_lr);
    return 0;
}

#include<stdio.h>
int main(){
	int N;
	char str[100];
	scanf("%d",&N);
	scanf("%s",str);

	int i;
	int num_u=0,num_d=0,num_l=0,num_r=0;
	for(i=0;i<N;i++) {
		if (str[i] == 'U'){
			num_u +=1;
		}
		if (str[i] == 'D'){
			num_d +=1;
		}
		if (str[i] == 'L'){
			num_l +=1;
		}
		if (str[i] == 'R'){
			num_r +=1;
		}
	}

	int num = 0;
	if (num_u < num_d){
		num += 2*num_u;
	}else{
		num += 2*num_d;
	}
	if (num_l < num_r){
		num += 2*num_l;
	}else{
		num += 2*num_r;
	}
	printf("%d",num);

}

#include<stdio.h>
int nnn(int a,int b)
{
return (a<b)?a:b;
}
int main()
{
    int n,left=0,right=0,up=0,down=0;
    scanf("%d",&n);
    char s[n+1];
    scanf("%s",s);
    s[n]='\0';
    for(int i=0;i<n;i++)
    {
    if(s[i]=='U')
    up++;
    else if(s[i]=='D')
    down++;
    else if(s[i]=='L')
    left++;
    else
    right++;
    }
    int max=(2*nnn(up,down))+(2*nnn(left,right));
    printf("%d",max);
    return 0;
}

#include<stdio.h>
#include<string.h>
int main(){
	int n,i,a=0,b=0,c=0,d=0,sum=0;
	char s[110];
	scanf("%d%s",&n,&s);
	for(i=0;i<n;i++){
		if(s[i]=='U') a++;
		if(s[i]=='D') b++;
		if(s[i]=='L') c++;
		if(s[i]=='R') d++;
	}
	if(a>b) sum+=2*b;
	else sum+=2*a;
	if(c>d) sum+=2*d;
	else sum+=2*c;
	printf("%d\n",sum);
	return 0;
}

#include <stdio.h>
#include <string.h>

int main(void) {
	int n,i;
	scanf("%d",&n);
	char a[101];
	scanf("%s", a);

	int x=0;
	int y=0;
	for (i=0;i<n;i++) {
		if (a[i]=='L')
			x++;
		else if (a[i]=='R')
			x--;
		else if (a[i]=='U')
			y++;
		else if (a[i]=='D')
			y--;
	}
	if (x<0)
		x=-x;
	if (y<0)
		y=-y;
	printf("%d",n-x-y);

	return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif

    int n,i,a,left=0,right=0,up=0,down=0,result;
    scanf("%d",&n);
    char str[n];
    scanf("%s",str);
    a=strlen(str);

    for(i=0;i<a;i++)
    {
    	if(str[i]=='L')
    	{
    		left++;
    	}
    	else if(str[i]=='R')
    	{
    		right++;
    	}
    	else if(str[i]=='U')
    	{
    	    up++;
    	}
    	else if(str[i]=='D')
    	{
    		down++;
    	}

    }

if(left<right)
{
	result=result+(left*2);
}
else
{
	result=result+(right*2);
}

if(up<down)
{
	result=result+(up*2);
}

else
{
	result=result+(down*2);
}

printf("%d\n",result );

}

#include<stdio.h>

int main()
{

	int n,i;
	int lcount=0,rcount=0,dcount=0,ucount=0,count=0;

	char a[105];

	scanf("%d",&n);
	scanf("%s",a);

	for(i=0;i<n;i++)
	{

		if(a[i] =='L')lcount++;

		if(a[i] =='R')rcount++;

		if(a[i] =='U')ucount++;

		if(a[i] =='D')dcount++;
	}

		if(lcount < rcount)
			count = count + 2 * lcount;

		if(lcount>=rcount)
			count = count + 2 * rcount;

		if(dcount<ucount)
			count=count + 2 * dcount;

		if(dcount>=ucount)
			count=count + 2 * ucount;

	printf("%d",count);

	return 0;
}

#include <stdio.h>
#include <string.h>

int main()
{
  int temp1, temp2, rep, i, U = 0, R = 0, D = 0, L = 0, tot;
  char comando;
  scanf("%d", &rep);
  for(i = 0; i < rep; i++)
  {
   scanf(" %c", &comando);
//    printf("comando: %c\n", comando);
    if(comando == 'U') U++;
    if(comando == 'R') R++;
    if(comando == 'D') D++;
    if(comando == 'L') L++;
  }
//  printf("U = %d, R = %d, D = %d, L = %d\n", U, R, D, L);
  temp1 = (U < D)? U: D;
  temp2 = (R < L)? R: L;

  printf("%d\n", (temp2 + temp1)*2);

  return 0;
}

/* 1514038923158 */

#include <stdio.h>

int main(void){
    int n;
    scanf("%d", &n);
    char udlr;
    int xf = 0;
    int yf = 0;
    for(int i = 0; i < n; ++i){
        scanf(" %c", &udlr);
        if(udlr == 'U'){
            ++yf;
        }
        else if(udlr == 'D'){
            --yf;
        }
        else if(udlr == 'R'){
            ++xf;
        }
        else if(udlr == 'L'){
            --xf;
        }
    }
    if(xf < 0){
        xf *= -1;
    }
    if(yf < 0){
        yf *= -1;
    }
    printf("%d\n", n-xf-yf);
    return 0;
}

/* 1515103045754 */

#include <stdio.h>
#include <string.h>
#include <math.h>

#define M 101
//#define N 26
#define MAX(a, b) (((a)>(b))?(a):(b))
#define MIN(a, b) (((a)>(b))?(b):(a))

int a[4];
int main() {
    int n;
    char s[M];
    scanf("%d%s", &n, s);
    for (int i = 0; i < n; ++i) {
        switch(s[i]) {
            case 'L' : a[0]++; break;
            case 'R' : a[1]++; break;
            case 'U' : a[2]++; break;
            case 'D' : a[3]++; break;
        }
    }
    printf("%d\n", (MIN(a[0], a[1]) + MIN(a[2], a[3])) *2);
    return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
int n,i,U,D,L,R,cmd,x,y;
U=L=D=R=cmd=x=y=0;
scanf("%d",&n);
char str[n];
scanf("%s",str);

for(i=0;i<n;i++)
{
   if(str[i]=='U') y++;
   else if(str[i]=='D') y--;
   else if(str[i]=='L') x--;
   else if(str[i]=='R') x++;
}
cmd=abs(x)+abs(y);
printf("%d\n",n-cmd);
return 0;
}

#include <stdio.h>
#include <math.h>

int main() {
    int n;
    scanf("%d", &n);
    int ct;
    int right, left, up, down;
    right = left = up = down = 0;
    for (ct = 0; ct <= n; ct++) {
        char temp;
        scanf("%c", &temp);
        if(temp == 'U'){
            up++;
        }
        if(temp == 'L'){
            left++;
        }
        if(temp == 'R'){
            right++;
        }
        if(temp == 'D'){
            down++;
        }
    }

    int minx, miny;
    if(right == left){
        minx = right;
    }else{
        minx = (right < left) ? right : left;
    }
    if(up == down){
        miny = up ;
    }else {
        miny = (up < down) ? up : down;
    }
    printf("%d", 2 * (minx + miny));

}

#include <stdio.h>
int com[150];
char s[1005];

int min(int a, int b)
{
    return a < b ? a : b;
}

int main()
{
    int n, i, tot;
    scanf("%d%s", &n, s);
    for(i = 0; i < n; ++i)
        ++com[s[i]];
    tot = 2 * (min(com['U'], com['D']) + min(com['L'], com['R']));
    printf("%d\n", tot);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
int saisie(int n)
{
    do
    {
        scanf("%d",&n);
    }while (!((n>=1)&&(n<=100)));
    return n;

}
int min(int x,int y)
{
    if (x<y)
        return x;
    else
        return y;
}
int main()
{
    int n,u=0,d=0,r=0,l=0;
    n=saisie(n);
    char ch[100];
    scanf("%s",ch);
    int i;
    for ( i=0;i<n;i++)
    {
        switch (ch[i])
        {
        case 'U':
            u++;
            break;
        case 'D':
            d++;
            break;
        case 'R':
            r++;
            break;
        case 'L':
            l++;
            break;
        }
    }
    printf("%d",2*min(u,d)+2*min(r,l));
    return 0;

}

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int hash[27]={},n,i,a,b,ans;
	char s[1000022];
	scanf("%d",&n);
	scanf("%s",s);
	i=0;
	while(s[i]!='\0')
	{
		hash[s[i]-'A']++;
		i++;
	}
	if(hash['R'-'A']>hash['L'-'A'])
		a=hash['L'-'A'];
	else
		a=hash['R'-'A'];
	if(hash['U'-'A']>hash['D'-'A'])
		b=hash['D'-'A'];
	else
		b=hash['U'-'A'];
    ans=a+b;
    ans*=2;
    printf("%d",ans);

}

#include <stdio.h>

#define MAX_COMMANDS 	100
#define ABS(x) 			((x < 0) ? (-x) : (x))

int		main(void)
{
	char commands[MAX_COMMANDS + 1];
	int num_commands;
	int lr_dist;
	int ud_dist;

	commands[MAX_COMMANDS] = '\0';

	scanf("%d\n", &num_commands);
	for (int i = 0; i < num_commands; i++)
		scanf("%c", &commands[i]);

	lr_dist = 0;
	ud_dist = 0;
	for (int i = 0; i < num_commands; i++)
	{
		if (commands[i] == 'L')
			lr_dist--;
		else if (commands[i] == 'R')
			lr_dist++;
		else if (commands[i] == 'U')
			ud_dist++;
		else if (commands[i] == 'D')
			ud_dist--;
	}
	printf("%d\n", num_commands - ABS(lr_dist) - ABS(ud_dist));
	return (0);
}

#include <stdio.h>
char s[101];
int min(int a,int b){return (a<b)?a:b;}
int main()
{
    int len,i;
    scanf("%d",&len);
    scanf("%s",s);
    int l,r,u,d;
    l=u=r=d=0;
    for(i=0;i<len;i++)
    {
        if(s[i]=='L') l++;
        else if(s[i]=='U') u++;
        else if(s[i]=='R') r++;
        else d++;
    }
    printf("%d\n",2*min(l,r)+2*min(u,d));
    return 0;
}

#include<stdio.h>
#include<math.h>
int main(){
    int n,i,x=0,y=0;
    scanf("%d",&n);
    char s[n+1];
    scanf("\n");
    gets(s);
    for (i=0;i<n;i++){
        if (s[i]=='L') x++;
        else if (s[i]=='R') x--;
        else if (s[i]=='U') y++;
        else if (s[i]=='D') y--;
    }
    printf("%d",n-abs(x)-abs(y));
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
  int n,i,j,k,l=0,m=0, c=0, a[2000];
  scanf("%d", &n);
  char h;
  for(i=0;i<n;i++){
    scanf(" %c", &h);
    if(h=='L')
      m--;
    else if(h=='R')
      m++;
    else if(h=='D')
      l--;
    else if(h=='U')
      l++;
  }
  printf("%d\n", n-abs(l)-abs(m));
  return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
    char s[105];
    int r,l,u,d,n,i,ans,ans1,total;
    while(~scanf("%d",&n))
    {
        getchar();
        scanf("%s",s);
        r=0,l=0,u=0,d=0;
        for(i=0;i<n;i++)
        {
            if(s[i]=='R')
                r++;
            else if(s[i]=='L')
                l++;
            else if(s[i]=='U')
            u++;
            else
                d++;
        }
        if(l>r)
            ans=r*2;
        else
            ans=l*2;
        if(u>d)
            ans1=d*2;
        else
            ans1=u*2;
            total=ans+ans1;
            printf("%d\n",total);
    }
}

#include <stdio.h>

int main(void)
{
    int n;
    scanf("%d", &n);
    int o[4] = {};
    char c;

    while (scanf("%c", &c) != EOF) {
        switch (c) {
            case 'L' : o[0]++;  break;
            case 'R' : o[1]++;  break;
            case 'U' : o[2]++;  break;
            case 'D' : o[3]++;  break;
        }
    }
    int minw = (o[0] > o[1]) ? o[1] : o[0];
    int minh = (o[2] > o[3]) ? o[3] : o[2];

    printf("%d\n", minw * 2 + minh * 2);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{ int n,i,l=0,r=0,u=0,d=0,M=0;
scanf("%d",&n);
char command[n];
    for(i=0;i<=n;i++)
        scanf("%c",&command[i]);
        for(i=0;i<=n;i++)
        {
            if(command[i]=='L')
                l++;
            if(command[i]=='R')
                r++;
            if(command[i]=='U')
                u++;
            if(command[i]=='D')
                d++;
        }
        if(r==l||r<l)
            M=M+2*r;
        else
            M=M+2*l;

        if(u==d||d<u)
            M=M+2*d;
        else
            M=M+2*u;

            printf("%d \n",M);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int x = 0, y = 0, n, i, s;
char d[3000];

int main()
{
    scanf("%d", &n);
    for (i = 0; i <= n; i++) scanf("%c", &d[i]);
    s = 0;
    for (i = 1; i <= n; i++)
        switch(d[i])
        {
            case 'U' : y++; break;
            case 'D' : y--; break;
            case 'L' : x--; break;
            case 'R' : x++; break;
        }
    s = n - abs(x) - abs(y);
    printf("%d", s);
    return 0;
}

#include <stdio.h>
int min(int a, int b) {
	if (a>b) return b;
	else return a;
}
int main() {
	int n;
	scanf("%d", &n);
	char s[n+1];
	int l=0, r=0, u=0, d=0;
	scanf("%s", s);
	for (int i=0;s[i]!='\0';i++) {
		if (s[i]=='L') l++;
		if (s[i]=='R') r++;
		if (s[i]=='U') u++;
		if (s[i]=='D') d++;
	}
	printf("%d\n", min(l, r)*2+min(u, d)*2);
}

#include<stdio.h>
#include<string.h>
int min(int a,int b)
{
    return a<b?a:b;
}
int main()
{
    int n,i,a[10];
    char c;
    int num=0;
    memset(a,0,sizeof(a));
    scanf("%d",&n);
    getchar();
    for(i=1;i<=n;i++)
    {
        scanf("%c",&c);
        if(c=='U') a[1]++;
        if(c=='D') a[2]++;
        if(c=='L') a[3]++;
        if(c=='R') a[4]++;
    }
    num=2*(min(a[1],a[2])+min(a[3],a[4]));
    printf("%d\n",num);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ABS(a)((a < 0) ? -1 * a : a)
int main()
{
	int dx=0,dy=0,n,i=0;
	scanf("%d",&n);
	char frase[n];
	scanf(" %s",frase);
	//printf("%d %s\n",n,frase);
	for(i=0;i < strlen(frase);i++)
	{
		if(frase[i] == 'U' )
		{
			++dx;
		}
		else if(frase[i] == 'D')
		{
			--dx;
		}
		else if(frase[i] == 'L')
		{
			--dy;
		}
		else if(frase[i] == 'R')
		{
			++dy;
		}
	}
	printf("%d",n-ABS(dx)-ABS(dy));
return 0;
}
/* 1513358766868 */

#include <stdio.h>

int double_of_min(int a, int b)
{
	int aux;
	if(a < b)
	{
		aux = a;
		a = b;
		b = aux;
	}
	return 2*b;
}

int main()
{
	int n, i;
	int u, d, l, r;

	scanf("%d", &n);
	getchar();

	u = 0;
	d = 0;
	l = 0;
	r = 0;

	char commands[n];

	for(i = 0; i < n; ++i)
	{
		scanf("%c", &commands[i]);
	}

	for(i = 0; i < n; ++i)
	{
		if(commands[i] == 'U') u++;
		if(commands[i] == 'D') d++;
		if(commands[i] == 'L') l++;
		if(commands[i] == 'R') r++;
	}

	printf("%d\n", double_of_min(u, d) + double_of_min(l, r));
	//printf("%s", commands);
	return 0;
}
/* 1512874373797 */

#include <stdio.h>
#include <math.h>

int main()
{

    int n, i, dx=0,dy=0;

    char str[105];

    scanf("%d",&n);
    scanf("%s",str);

    for(i=0; i<n; i++)
    {
        if(str[i]=='R')
            dx++;
        else if(str[i]=='L')
            dx--;
        else if(str[i]=='U')
            dy++;
        else
            dy--;
    }
    printf("%d\n",(n-abs(dx)-abs(dy)));

    return 0;
}

#include <stdio.h>
#include <string.h>

int main()
{
    int n;
    scanf("%d\n",&n);
    char a[n];
     gets(a);
    int i,dx=0,dy=0;
    for(i=0;i<n;i++)
    {
        switch(a[i])
        {
          case 'U':
            dy++;
            break;
          case 'u' :
            dy++;
            break;
          case 'L' :
            dx--;
            break;
          case 'l' :
            dx--;
            break;
          case 'd' :
            dy--;
            break;
          case 'D' :
            dy--;
            break;
          case 'r' :
            dx++;
            break;
          case 'R' :
            dx++;
            break;
        }
    }
    printf("%d",n-abs(dx)-abs(dy));
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
int main()
{
    unsigned u,d,r,l,i,n,ud,lr;
    char *s;

    scanf("%u",&n);
    s=(char *)malloc(n+1);
    scanf("%s",s);
    for(i=0,r=0,l=0,d=0,u=0;i<n;i++)
    {
        if(s[i]=='U')
            u++;
        else if (s[i]=='D')
            d++;
        else if (s[i]=='R')
            r++;
            else if (s[i]=='L')
            l++;
    }
    if(u<d)
        ud=u;
    else
        ud=d;
    if(l<r)
        lr=l;
    else
        lr=r;
    printf("%u",lr*2+ud*2);
}

#include <stdio.h>
#define min(x,y) (((x) < (y)) ? (x) : (y))
int main()
{
    int n,i,u=0,d=0,r=0,l=0;
    scanf("%d",&n);
    char arr[n];
    scanf("%s",&arr);
    for (i=0;i<n;i++){
            switch (arr[i]){
                case 'U':
                u++; break;
                case 'D':
                d++; break;
                case 'R':
                r++; break;
                case 'L':
                l++; break;
                }
        }

   printf("%d",2*(min(u,d)+min(r,l)));

 return 0;
}

#include <stdio.h>
int com[150];
char s[1005];

int min(int a, int b)
{
    return a < b ? a : b;
}

int main()
{
    int n, i, tot;
    scanf("%d%s", &n, s);
    for(i = 0; i < n; ++i)
        ++com[s[i]];
    tot = 2 * (min(com['U'], com['D']) + min(com['L'], com['R']));
    printf("%d\n", tot);
    return 0;
}

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n, u, d, l, r, resp, j;
    resp = u = d = l = r;
    scanf("%d\n", &n);
    char c;
    for(j = 0; j < n; j++) {
        scanf("%c", &c);
        switch(c) {
            case 'U' : u++; break;
            case 'D' : d++; break;
            case 'L' : l++; break;
            case 'R' : r++; break;
        }
        if((u > 0) && (d > 0)) {
            resp+=2;
            u--;
            d--;
        }
        if((l > 0) && (r > 0)) {
            resp+=2;
            l--;
            r--;
        }
    }
    printf("%d", resp);
	return 0;
}
/* 1512824806805 */

#include <stdio.h>

#define N	100

int main() {
	static char cc[N + 1];
	int n, i, ku, kd, kl, kr;

	scanf("%d%s", &n, cc);
	ku = kd = kl = kr = 0;
	for (i = 0; i < n; i++)
		if (cc[i] == 'U')
			ku++;
		else if (cc[i] == 'D')
			kd++;
		else if (cc[i] == 'L')
			kl++;
		else if (cc[i] == 'R')
			kr++;
	printf("%d\n", ((ku < kd ? ku : kd) + (kl < kr ? kl : kr)) * 2);
	return 0;
}

#include <stdio.h>

#define N 100
#define min(A, B) ((A) < (B) ? (A) : (B))

int main() {
	static char s[N + 1];
	int n, i, cnt1, cnt2, cnt3, cnt4;

	scanf("%d%s", &n, s);
	cnt1 = cnt2 = cnt3 = cnt4 = 0;
	for (i = 0; i < n; i++)
		if (s[i] == 'U')
			cnt1++;
		else if (s[i] == 'D')
			cnt2++;
		else if (s[i] == 'L')
			cnt3++;
		else
			cnt4++;
	printf("%d\n", (min(cnt1, cnt2) + min(cnt3, cnt4)) * 2);
	return 0;
}

#include<stdio.h>
int main()
{
    int n,a1=0,a2=0,a3=0,a4=0,sum=0,i;
    scanf("%d",&n);
    char a[n];
    scanf("%s",a);
    for(i=0;i<n;i++)
    {
        if(a[i]=='L') a1++;
        else if(a[i]=='R') a2++;
        else if(a[i]=='U') a3++;
        else a4++;
    }
    if(a1>=a2) sum=sum+a2;
    else sum=sum+a1;
    if(a3>=a4) sum=sum+a4;
    else sum=sum+a3;
    printf("%d",sum*2);
}

#include <stdio.h>
int min(int a,int b)
{
	if(a>b) return b;
	else return a;
}
int main(){
	int i,n,ans;
	char s[100];
	int a[4]={0};
	scanf("%d%s",&n,s);
	for(i=0;i<n;i++){
		switch(s[i]){
		case'L':a[0]++;break;
	    case'R':a[1]++;break;
	    case'U':a[2]++;break;
	    case'D':a[3]++;break;
		}
	}
	ans=2*min(a[0],a[1])+2*min(a[2],a[3]);
	printf("%d",ans);
}

#include <stdio.h>

int main(){
	int i,n;
	char OP;
	int result = 0;
	int L=0,R=0,D=0,U=0;

	scanf(" %d",&n);

	for(i=0;i<n;i++){
		scanf(" %c",&OP);
		if(OP == 'D') D++;
		if(OP == 'U') U++;
		if(OP == 'L') L++;
		if(OP == 'R') R++;
	}

	if(L > R)
		result += (R*2);
	else
		result += (L*2);

	if(U > D)
		result += (D*2);
	else
		result += (U*2);

	printf("%d\n", result);

	return 0;
}
/* 1512828846855 */

#include <stdio.h>
#define min(a,b) a>=b ? b : a
int main()
{
	int n,i,L=0,R=0,U=0,D=0;
	char move[101];
	int count1=0,count2=0;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf(" %c",&move[i]);
		if(move[i]=='L')
			L++;
		if(move[i]=='R')
			R++;
		if(move[i]=='U')
			U++;
		if(move[i]=='D')
			D++;
	}
	//printf("%d %d %d %d\n",L,R,U,D);
	if(L>0 && R>0)
	{
		count1 = min(L,R);
	}
	if(U>0 && D>0)
	{
		count2 = min(U,D);
	}
	//printf("1.%d 2.%d\n",count1,count2);
	printf("%d\n",2*(count1+count2));
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,n,nU,nL,nR,nD,c;
    nU=0; nL=0; nR=0; nD=0; c=0;
    scanf("%d",&n);
    char s[n];
    for(i=0;i<n;i++)
    {
        scanf("%s",&s[i]);
        if(s[i]=='U')
            nU++;
            else if(s[i]=='L')
                nL++;
            else if(s[i]=='R')
                nR++;
            else
                nD++;
    }
    if(nL<=nR)
        c=2*nL;
    else
        c=2*nR;
    if(nD<=nU)
        c=c+(2*nD);
    else
        c=c+(2*nU);
        printf("%d",c);
    return 0;
}

#include<stdio.h>
int main()
{
	int n,count=0;
	scanf("%d",&n);
	char arr[n+1];
	scanf("%s",arr);
	int l=0,r=0,u=0,d=0;
	for (int i = 0; i < n; ++i)
	{
		if (arr[i]=='L')
		{
			l++;
		}
		if (arr[i]=='R')
		{
			r++;
		}
		if (arr[i]=='U')
		{
			u++;
		}
		if (arr[i]== 'D')
		{
			d++;
		}
	}

	if (l>r)
	{
		count+=r;
	}
	else
		count+=l;
	if (u>d)
	{
		count+=d;
	}
	else
		count+=u;
	printf("%d\n",2*count );
		}

#include <stdio.h>
#include <math.h>
char s[110];

int main() {
	int n, i;
	int x = 0;
	int y = 0;
	int o = 0;
	scanf("%d", &n);
	scanf("%s", s);
	for (i=0; i<n; i++) {
		if (s[i] == 'L') {
			x--;
		} else if (s[i] == 'R') {
			x++;
		} else if (s[i]=='U') {
			y++;
		} else if (s[i]=='D') {
			y--;
		}
	}
	o = n-abs(x)-abs(y);
	printf("%d\n", o);
	return 0;
}

#include <stdio.h>

int min(int a, int b)
{
    return a < b ? a : b;
}

int main()
{
    int n, max;
    char str[101];
    int U, D, L, R;
    max = U = D = L = R = 0;
    scanf("%d %s", &n, str);
    for(int i = 0; i < n; ++i) {
        if(str[i] == 'U') {
            ++U;
        } else if(str[i] == 'D') {
            ++D;
        } else if(str[i] == 'L') {
            ++L;
        } else {
            ++R;
        }
    }
    printf("%d\n", min(U, D) * 2 + min(L, R) * 2);
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
int main(){
    int n,min;
    scanf("%d",&n);
    // char Command[]=malloc((n+1)*sizeof(char));  // does it work? /// NO!!!
    char* Command=malloc((n+1)*sizeof(char));
    scanf("%s",Command);
    int LR[2]={0,0};
    int DU[2]={0,0};
    for(int i=0;i<n;i++){
        if(Command[i]=='L'){LR[0]++;}
        else if(Command[i]=='D'){DU[0]++;}
        else if(Command[i]=='U'){DU[1]++;}
        else{LR[1]++;}
    }
    min=(LR[0]<LR[1])?LR[0]:LR[1];
    int ans=2*min;
    min=(DU[0]<DU[1])?DU[0]:DU[1];
    ans+=min*2;
    printf("%d\n",ans);
}

#include <stdio.h>
int min(int x, int y) {
    return (x<=y) ? x:y;
}
int move(int length) {
    int save[]={0,0,0,0};
    char curr;
    for (int i=0; i<length; i++) {
        scanf("%c",&curr);
        switch (curr) {
            case 'U':
                save[0]++;
                break;
            case 'D':
                save[1]++;
                break;
            case 'L':
                save[2]++;
                break;
            case 'R':
                save[3]++;
                break;
        }
    }
    return 2*(min(save[0],save[1])+min(save[2],save[3]));
}
int main() {
    int len; scanf("%d\n",&len);
    printf("%d",move(len));
    return 0;
}

#include<stdio.h>
#include<string.h>
int main(){
	int n,i,a=0,b=0,c=0,d=0,sum=0;
	char s[110];
	scanf("%d%s",&n,&s);
	for(i=0;i<n;i++){
		if(s[i]=='U') a++;
		if(s[i]=='D') b++;
		if(s[i]=='L') c++;
		if(s[i]=='R') d++;
	}
	if(a>b) sum+=2*b;
	else sum+=2*a;
	if(c>d) sum+=2*d;
	else sum+=2*c;
	printf("%d\n",sum);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define MAXN 1005

int main()
{
    int T, i, L, D, U, R;
    char str[MAXN];
    scanf("%d", &T);
    T = 1;
    while (T--)
    {
        scanf("%s", str);
        L = D = U = R = 0;
        for (i = 0; str[i]; ++i)
        {
            if (str[i] == 'L')
                ++L;
            else if (str[i] == 'D')
                ++D;
            else if (str[i] == 'U')
                ++U;
            else
                ++R;
        }
        printf("%d\n", 2 * (min(L, R) + min(U, D)));
    }
    return 0;
}

#include<stdio.h>
#include<math.h>
int main(){
    int n,i,x=0,y=0;
    scanf("%d",&n);
    char s[n+1];
    scanf("\n");
    gets(s);
    for (i=0;i<n;i++){
        if (s[i]=='L') x++;
        else if (s[i]=='R') x--;
        else if (s[i]=='U') y++;
        else if (s[i]=='D') y--;
    }
    printf("%d",n-abs(x)-abs(y));
}

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int min(int a, int b)
{
    if (a>b)
        return b;
    return a;
}
int Buggy_Robot(int n, char seq[])
{
    int i,u=0,d=0,l=0,r=0;
    for (i=0;i<n;i++)
    {
        if (seq[i]=='U')
                u+=1;
        else if (seq[i]=='D')
                 d+=1;
             else if (seq[i]=='L')
                  l+=1;
                else
                     r+=1;

    }
    return (2*min(l,r)+2*min(u,d));

}
int main()
{
    int n;
    char seq[101];
    scanf("%d",&n);
    scanf("%s",seq);
    printf("%d",Buggy_Robot(n,seq));

    return 0;
}

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    getchar();
    char c;
    int up,down,left,right;
    up=down=left=right=0;
    while(n--)
    {
        scanf("%c",&c);
        switch(c)
        {
            case 'U':up++;break;
            case 'D':down++;break;
            case 'L':left++;break;
            case 'R':right++;
        }
    }
    getchar();
    int sum=0;
    if(up>down) sum+=down*2;
    else sum+=up*2;
    if(left>right) sum+=right*2;
    else sum+=left*2;
    printf("%d\n",sum);
    return 0;
}

#include<stdio.h>
int min(int a,int b)
{return a>b ? b:a;}
int main()
{
  int n,i,l=0,r=0,u=0,d=0;
  scanf("%d",&n);
  char s[200];
  scanf("%s",s);
  for(i=0;i<n;i++)
  {
   if(s[i]=='L')
    l++;
   else if(s[i]=='R')
    r++;
   else if(s[i]=='U')
    u++;
   else if(s[i]=='D')
    d++;
  }
  printf("%d\n",2*(min(d,u)+min(l,r)));
 return 0;
}

#include<stdio.h>
int main()
{
    int n,i=0,num=0,cum=0,sum=0,pum=0,a=0;
    scanf("%d",&n);
    char s[n+1];
    scanf("%s",&s);
    while(s[i]!=0)
    {
        if(s[i]=='L')
        {
            num=num+1;
        }
        else if(s[i]=='R')
        {
            cum=cum+1;
        }
        else if(s[i]=='U')
        {
            pum=pum+1;
        }
        else if(s[i]=='D')
        {
            sum=sum+1;
        }
        i=i+1;
    }
    if(num>=cum)
    {
        a=a+cum+cum;
    }
    else
    {
        a=a+num+num;
    }
    if(pum>=sum)
    {
        a=a+sum+sum;
    }
    else
    {
        a=a+pum+pum;
    }
    printf("%d",a);
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ABS(a)((a < 0) ? -1 * a : a)
int main()
{
	int l=0,d=0,r=0,u=0,n,i=0;
	scanf("%d",&n);
	char frase[n];
	scanf(" %s",frase);
	//printf("%d %s\n",n,frase);
	for(i=0;i < strlen(frase);i++)
	{
		if(frase[i] == 'U' )
		{
			++u;
		}
		else if(frase[i] == 'D')
		{
			--d;
		}
		else if(frase[i] == 'L')
		{
			--l;
		}
		else if(frase[i] == 'R')
		{
			++r;
		}
	}
	u += d;
	r += l;
	printf("%d",n-u-r);
return 0;
}

#include <stdio.h>

int main()
{
    int n,i;
    scanf("%d",&n);
    char arr[n];
    scanf("%s",&arr);

    goto check;

check : {
    int u=0,d=0,r=0,l=0;
    for (i=0;i<n;i++){
            switch (arr[i]){
                case 'U':
                u++; break;
                case 'D':
                d++; break;
                case 'R':
                r++; break;
                case 'L':
                l++; break;
                }
        }
         if (u==d&&r==l){ printf("%d",n);}
         else{n--;  goto check;}
}
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	int x = 0,  y = 0;
	int n, max = 0;

	scanf("%d", &n);
	getchar();
	char comandos[n];
	scanf("%s",comandos);
	int i;
	for(i = 0; i < strlen(comandos); i++)
	{
		if(comandos[i] == 'U') y++;
		if(comandos[i] == 'D') y--;
		if(comandos[i] == 'L') x--;
		if(comandos[i] == 'R') x++;

		if(x == 0 && y == 0) max = i + 1;

	}

	printf("%d\n",max );

}
/* 1512826328718 */

#include <stdio.h>

int main(void) {
	int n;
	scanf("%d", &n);
	char udlr;
	int xf = 0;
	int yf = 0;
	for(int i = 0; i < n; ++i){
		scanf(" %c", &udlr);
		if(udlr == 'U'){
			++yf;
		}
		else if(udlr == 'D'){
			--yf;
		}
		else if(udlr == 'R'){
			++xf;
		}
		else if(udlr == 'L'){
			--xf;
		}
	}
	printf("%d\n", n-xf-yf);
	return 0;
}

/* 1515093055897 */

#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
	int pos_x = 0, pos_y = 0;

	int max = 0;

	int n;
	scanf("%d", &n);

	getchar();

	char comando[100];
	scanf("%[^\n]", comando);

	int i;
	for (i = 0; i < n; i++)
	{
		if (comando[i] == 'U') pos_y++;
		else if (comando[i] == 'D') pos_y--;
		else if (comando[i] == 'R') pos_x++;
		else pos_x--;

		if (pos_x == 0 && pos_y == 0) max = i+1;
	}

	printf("%d\n", max );

	return 0;
}
/* 1512828316954 */

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int Buggy_Robot(int n, char seq[])
{
    int p=0,i,u=0,d=0,l=0,r=0;
    for (i=0;i<n;i++)
    {
        if (seq[i]=='U')
                u+=1;
        else if (seq[i]=='D')
                 d+=1;
             else if (seq[i]=='L')
                  l+=1;
                else
                     r+=1;

        if (u==d && l==r )
            {
                u=r=d=l=0;
                p=i+1;

            }
    }
    return p;

}
int main()
{
    int n;
    char seq[101];
    scanf("%d",&n);
    scanf("%s",seq);
    printf("%d",Buggy_Robot(n,seq));

    return 0;
}

#include <stdio.h>
#include <string.h>

int main()
{

int x = 0, y = 0, aux = 0, tam, rep, i, cont = 0;
scanf("%d", &rep);
char comando[100];
scanf(" %s", comando);
tam = strlen(comando);

	for(i = 0; i < rep; i++)
	{
		if(comando[i] == 'U') y++;
		else if(comando[i] == 'D') y--;
		else if(comando[i] == 'L') x--;
		else x++;

		if(x == 0 && y == 0)
		  if(rep > aux) aux = rep;
	}

printf("%d", aux);

return 0;

}

/* 1512828791924 */

#include <stdio.h>

int main(){
	int i,n;
	int x=0,y=0,count=0,flag=0;
	char OP;

	scanf(" %d",&n);
	for(i=0;i<n;i++){
		scanf(" %c",&OP);
		if(OP == 'D') y--;
		if(OP == 'U') y++;
		if(OP == 'L') x--;
		if(OP == 'R') x++;
		count++;

		if(!x && !y){
			flag = 1;
			break;
		}
	}

	if(flag)
		printf("%d\n", count);
	else
		printf("0\n");

	return 0;
}

#include <stdio.h>

int main(){
  int n; scanf("%d ", &n);

  int x=0, y=0, cnt=0, ans=0;
  while(n--){
    char ch;
    ch = getchar();
    if (ch=='U') y++;
    else if (ch=='D') y--;
    else if (ch=='L') x--;
    else if (ch=='R') x++;
    cnt++;
    if( x==0 && y==0){
      if (cnt>ans) ans=cnt;
      cnt=0;
    }
  }
  printf("%d\n", ans);
}

#include <stdio.h>
#define rep(i,a,b) for (int i=a; i<b; i++)
int max(int x, int y) {
    return (x>y) ? x:y;
}
int move(int length) {
    int x=0,y=0,maxlen=0,curlen=1;
    char curr;
    rep (i,0,length) {
        scanf("%c",&curr);
        switch (curr) {
            case 'U':
                y++;
                break;
            case 'D':
                y--;
                break;
            case 'L':
                x--;
                break;
            case 'R':
                x++;
                break;
        }
        if (x==0 && y==0) {
            maxlen=max(maxlen,curlen);
            curlen=1;
        }
        curlen++;
    }
    return maxlen;
}
int main() {
    int len; scanf("%d\n",&len);
    printf("%d",move(len));
    return 0;
}

#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
	int pos_x = 0, pos_y = 0;

	int max = 0;

	int n;
	scanf("%d", &n);

	getchar();

	char comando[100];
	scanf("%[^\n]", comando);

	int i;
	for (i = 0; i < n; i++)
	{
		if (comando[i] == 'U') pos_y++;
		else if (comando[i] == 'D') pos_y--;
		else if (comando[i] == 'R') pos_x++;
		else if (comando[i] == 'L') pos_x--;

		if (pos_x == 0 && pos_y == 0) max = i+1;
	}

	printf("%d", max );

	return 0;
}
/* 1512828801534 */

#include <stdio.h>

int main()
{
    int n, max, x, y;
    char str[101];
    x = y = max = 0;
    scanf("%d %s", &n, str);
    for(int i = 0; i < n; ++i) {
        if(str[i] == 'U') {
            ++y;
        } else if(str[i] == 'D') {
            --y;
        } else if(str[i] == 'L') {
            --x;
        } else {
            ++x;
        }
        if(x == 0 && y == 0) {
            if(max < i + 1) {
                max = i + 1;
            }
        }
    }
    printf("%d\n", max);
    return 0;
}

#include <stdio.h>

#define MAX_COMMANDS 100

int		main(void)
{
	char commands[MAX_COMMANDS + 1];
	int num_commands;
	int max_poss_commands;
	int lr_dist;
	int ud_dist;

	for (int i = 0; i < MAX_COMMANDS; i++)
		commands[i] = '0';
	commands[MAX_COMMANDS] = '\0';

	scanf("%d", &num_commands);
	for (int i = 0; i < num_commands + 1; i++)
		scanf("%c", &commands[i]);

	lr_dist = 0;
	ud_dist = 0;
	max_poss_commands = 0;
	for (int i = 0; i < num_commands + 1; i++)
	{
		if (commands[i] == 'L')
			lr_dist--;
		else if (commands[i] == 'R')
			lr_dist++;
		else if (commands[i] == 'U')
			ud_dist++;
		else if (commands[i] == 'D')
			ud_dist--;

		if (lr_dist == 0 && ud_dist == 0 && i > max_poss_commands)
			max_poss_commands = i;
	}
	printf("%d\n", max_poss_commands);
	return (0);
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	int x = 0,  y = 0;
	int n, max = 0;

	scanf("%d", &n);
	getchar();
	char comandos[n];
	scanf("%s",comandos);
	int i;
	for(i = 0; i < strlen(comandos); i++)
	{
		if(comandos[i] == 'U') y++;
		if(comandos[i] == 'D') y--;
		if(comandos[i] == 'L') x--;
		if(comandos[i] == 'R') x++;

		if(x == 0 && y == 0) max = i + 1;

	}

	printf("%d\n",max );

}
/* 1512826290791 */

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int Buggy_Robot(int n, char seq[])
{
    int i,u=0,d=0,l=0,r=0;
    for (i=0;i<n;i++)
    {
        switch (seq[i])
        {
        case 'U':
            u+=1;
            break;
        case 'D':
            d+=1;
            break;
        case 'L':
            l+=1;
            break;
        case 'R':
            r+=1;
            break;
        default :
            printf("unidentified request\n");
            break;

        }
        if (u==d && l==r )
            break;
    }
    if (i<n)
        return i+1;
    else
        return 0;

}
int main()
{
    int n;
    char seq[100];
    printf("Enter number of commands\n");
    scanf("%d",&n);
    printf("Enter seuence with the length %d \n",n);
    scanf("%s",seq);
    printf("%d",(Buggy_Robot(n,seq)));

    return 0;
}

#include <stdio.h>

int main()
{
    int i, n, x, y, t;
    char c[101] = { 0 };

    scanf("%d ", &n);

    scanf("%s ", c);

    x = 0;
    y = 0;
    t = 0;
    for (i = 0; i < n; ++i) {
        switch (c[i]) {
            case 'U':
                y += 1;
                break;
            case 'D':
                y -= 1;
                break;
            case 'L':
                x -= 1;
                break;
            case 'R':
                x += 1;
                break;
            default:
                printf("what");
        }

        if (x == 0 && y == 0) {
            t = i + 1;
        }
    }

    printf("%d\n", t);

    return 0;
}

#include <stdlib.h>
#include <stdio.h>

int main()
{
    int n, count = 0, L = 0, U = 0 , D = 0, R = 0, i;

    scanf("%d", &n);

    for(i = 0; i<n; i++)
    {
        char c;
        scanf("%c", &c);
        switch(c)
        {
            case 'L':
                L++;
                break;
            case 'D':
                D++;
                break;
            case 'U':
                U++;
                break;
            case 'R':
                R++;
                break;
        }
    }
    count = (L<R?L*2:R*2) + (U<D?U*2:D*2);
    printf("%d", count);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MIN(a,b)((a < b) ? a : b )
#define ABS(a)((a < 0) ? -1 * a : a)
int main()
{
	int l=0,d=0,r=0,u=0,n,i=0;
	scanf("%d",&n);
	char frase[n];
	scanf(" %s",frase);
	//printf("%d %s\n",n,frase);
	for(i=0;i < strlen(frase);i++)
	{
		if(frase[i] == 'U' )
		{
			++u;
		}
		else if(frase[i] == 'D')
		{
			--d;
		}
		else if(frase[i] == 'L')
		{
			--l;
		}
		else if(frase[i] == 'R')
		{
			++r;
		}
	}
	int minx = MIN(l,r);
	int miny = MIN(u,d);
	printf("%d\n",ABS((minx+miny)*2));

return 0;
}
/* 1512830800117 */

#include<stdio.h>
#define min(a,b) ((a)<(b))?a:b
int main()
{

	int l=0,r=0,u=0,d=0;
	char c;
	int i,n;
	scanf("%d",&n);

	for(i=0;i<n;i++)
	{

	   scanf("%c",&c);
	   switch (c)
	   {

	   case  'U' :u++;break;
	   case  'D' :d++;break;
	   case  'L' :l++;break;
	   case  'R' :r++;break;
       }

    }
    r=min(l,r);
    d=min(u,d);
   // printf("l:%d r:%d u:%d d��%d\n",l,r,u,d) ;
    printf("%d",2*(r+d));
	return 0;
 }

#include <stdio.h>

int main(){
  int n; scanf("%d ", &n);

  int x=0, y=0, cnt=0, ans=1<<25;
  while(n--){
    char ch;
    ch = getchar();
    if (ch=='U') y++;
    else if (ch=='D') y--;
    else if (ch=='L') x--;
    else if (ch=='R') x++;
    cnt++;
    if( x==0 && y==0){
      if (cnt<ans) ans=cnt;
      cnt=0;
    }
  }
  printf("%d\n", ans);
}

#include<stdio.h>

int main()
{
    int Comandos, Linha = 0, Coluna = 0, i, Contagem = 0, Resposta=0;
    scanf("%d", &Comandos);
    getchar();

    char str[Comandos+1];
    fgets(str,Comandos+1,stdin);

    for (i=0;i<Comandos;i++)
    {
        if(str[i]=='L')
        {
            Linha-=1;
        }
        else if(str[i]=='D')
        {
            Coluna-=1;
        }
        else if(str[i]=='U')
        {
            Coluna+=1;
        }
        else if(str[i]=='R')
        {
            Linha+=1;
        }
        Contagem++;
        if(Linha == 0 && Coluna == 0)
        {
            Resposta = Contagem;
        }
    }

    printf("%d\n", Resposta);

    return 0;
}

/* 1513355789816 */

#include <stdio.h>

int main(void) {
	int n;
	scanf("%d", &n);
	char udlr;
	int xf = 0;
	int yf = 0;
	for(int i = 0; i < n; ++i){
		scanf(" %c", &udlr);
		if(udlr == 'U'){
			++yf;
		}
		else if(udlr == 'D'){
			--yf;
		}
		else if(udlr == 'R'){
			++xf;
		}
		else if(udlr == 'L'){
			--xf;
		}
	}
	printf("%d\n", n-xf-yf);
	return 0;
}

/* 1515093055897 */

#include <stdio.h>

int main(){
    int n,count = 0;
    int boo = 0;
    scanf("%d",&n);
    getchar();
    char cm[n];
    for(int i = 0; i<n; i++){
        scanf("%c",&cm[i]);
    }
    for(int i = 0; i<n-1; i++){
        boo = 0;
        for(int j = i+1; j<n; j++){
            if(cm[i]=='L')
                if(cm[j]=='R')
                    boo = 1;
            if(cm[i]=='R')
                if(cm[j]=='L')
                    boo = 1;
            if(cm[i]=='D')
                if(cm[j]=='U')
                    boo = 1;
            if(cm[i]=='U')
                if(cm[j]=='D')
                    boo = 1;
        }
        if(boo) count++;
    }
    printf("%d",2*count);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int x = 0, y = 0, n, i, s;
char d[101];

int main()
{
    scanf("%d", &n);
    if ((n >= 1) && (n <= 100))
    {
        n++;
        for (i = 1; i <= n; i++) scanf("%c", &d[i]);
        s = 0;
        for (i = 1; i <= n; i++)
        {
            switch(d[i])
            {
                case 'U' : y++; break;
                case 'D' : y--; break;
                case 'L' : x--; break;
                case 'R' : x++; break;
            }
            if ((x == 0) && (y == 0)) s = i;
        }
        s--;
        printf("%d", s);
    }
    return 0;
}

#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
	int pos_x = 0, pos_y = 0;

	int max = 0;

	int n;
	scanf("%d", &n);

	getchar();

	char comando[100];
	scanf("%[^\n]", comando);

	int i;
	for (i = 0; i < n; i++)
	{
		if (comando[i] == 'U') pos_y++;
		else if (comando[i] == 'D') pos_y--;
		else if (comando[i] == 'R') pos_x++;
		else if (comando[i] == 'L') pos_x--;

		if (pos_x == 0 && pos_y == 0) max = i+1;
	}

	printf("%d\n", max );

	return 0;
}
/* 1512828513351 */

#include <stdio.h>

int main()
{
    char c;
    int i, n, x, y, t;

    scanf("%d ", &n);

    x = 0;
    y = 0;
    t = 0;
    for (i = 0; i < n; ++i) {
        c = getchar();

        switch (c) {
            case 'U':
                ++y;
                break;
            case 'D':
                --y;
                break;
            case 'L':
                --x;
                break;
            case 'R':
                ++x;
                break;
        }

        if (x == 0 && y == 0) {
            t = i + 1;
        }
    }

    printf("%d\n", t);

    return 0;
}

#include <stdio.h>
#define rep(i,a,b) for (int i=a; i<b; i++)
int move(int length) {
    int x=0,y=0,max=0;
    char curr;
    rep (i,1,length+1) {
        scanf("%c",&curr);
        switch (curr) {
            case 'U':
                y++;
                break;
            case 'D':
                y--;
                break;
            case 'L':
                x--;
                break;
            case 'R':
                x++;
                break;
        }
        if (x==0 && y==0) max=i;
    }
    return max;
}
int main() {
    int len; scanf("%d\n",&len);
    printf("%d",move(len));
    return 0;
}

#include<stdio.h>

int main(){
	int s;
	scanf(" %d",&s);
	char ac[s+1];
	scanf(" %s",ac);
	int max = 0;
	for(int j = 0; j < s;++j){
		int ans = 0;
		int x = 0, y = 0;
		for(int i = j; i < s;++i){
			char a = ac[i];
			if(a == 'R')
				++x;
			if(a == 'U')
				++y;
			if(a == 'D')
				--y;
			if(a == 'L')
				--x;
			if(x == 0 && y == 0)
				ans = i+1;
		//printf("i es %d x es %d y es %d\n",i,x,y);
		//puts("");
		}
		if(ans > max)
			max = ans;
	}
	printf("%d\n",max);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	int x = 0,  y = 0;
	int n, max = 0;

	scanf("%d", &n);
	getchar();
	char comandos[n];
	scanf("%s",comandos);
	int i;
	for(i = 0; i < strlen(comandos); i++)
	{
		if(comandos[i] == 'U') y++;
		if(comandos[i] == 'D') y--;
		if(comandos[i] == 'L') x--;
		if(comandos[i] == 'R') x++;

		if(x == 0 && y == 0) max = i + 1;

	}

	printf("%d\n",max );

}
/* 1512826270114 */

#include <stdio.h>
#include <string.h>
int main()
{
	int contador=0,com=0,n,i=0;
	scanf("%d",&n);
	char frase[n];
	scanf(" %s",frase);
	//printf("%d %s\n",n,frase);
	for(i=0;i < strlen(frase);i++)
	{
		if(frase[i] == 'U' )
		{
			++contador;
		}
		else if(frase[i] == 'D')
		{
			--contador;
		}
		else if(frase[i] == 'L')
		{
			--contador;
		}
		else if(frase[i] == 'R')
		{
			++contador;
		}
		if(contador == 0) com += i+1;
	}
	printf("%d\n",com);

return 0;
}
/* 1512829803627 */

#include<stdio.h>
int main()
{
	int sumu=0,sumd=0,suml=0,sumr=0,sum=0,k,n,sumaw[100],tmp=0;
	char t[102];
	scanf("%d",&n);
	getchar();
	gets(t);
	for(k=0;k<n;k++)
	{
		if(t[k]=='U')
		sumu++;
		if(t[k]=='D')
		sumd++;
		if(t[k]=='L')
		suml++;
		if(t[k]=='R')
		sumr++;
		sum++;
		if(sumu==sumd&&sumr==suml)
		{
			tmp++;
			sumaw[tmp]=sum;
		}
	}
	printf("%d",sumaw[tmp]);
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char str[10000];

int main(){
int n,i,j,k,l,d,u,r,m,x=0,y=0,L;
l=0;d=0;r=0;u=0;
scanf("%d%s",&n,str);
L = strlen(str);

for(i=0;i<L;i++){
if(str[i]=='U')
y++;
else if(str[i]=='D')
y--;
else if(str[i]=='L')
x--;
else if(str[i]=='R')
x++;
}
int count = n-x-y;
printf("%d\n",count);
return 0;
}

#include <stdio.h>

/*
 * http://codeforces.com/contest/888/problem/C
 */
int main()
{
    int i, n, x, y, t;
    char c[101] = { 0 };

    scanf("%d ", &n);

    scanf("%s ", c);

    x = 0;
    y = 0;
    t = 0;
    for (i = 0; i < n; ++i) {
        switch (c[i]) {
            case 'U':
                y += 1;
                break;
            case 'D':
                y -= 1;
                break;
            case 'L':
                x -= 1;
                break;
            case 'R':
                x += 1;
                break;
            default:
                printf("what");
        }

        if (x == 0 && y == 0) {
            t = i + 1;
        }
    }

    printf("%d\n", t);

    return 0;
}

#include <stdio.h>

char s[110];

int main() {
	int n, i;
	int x = 0;
	int y = 0;
	int o = 0;
	scanf("%d", &n);
	scanf("%s", s);
	for (i=0; i<n; i++) {
		if (s[i] == 'L') {
			x--;
		} else if (s[i] == 'R') {
			x++;
		} else if (s[i]=='U') {
			y++;
		} else if (s[i]=='D') {
			y--;
		}
	}
	o = n-x-y;
	printf("%d\n", o);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	int x = 0,  y = 0;
	int n, max = 0;

	scanf("%d", &n);
	getchar();
	char comandos[n];
	scanf("%s",comandos);
	int i;
	for(i = 0; i < strlen(comandos); i++)
	{
		if(comandos[i] == 'U') y++;
		if(comandos[i] == 'D') y--;
		if(comandos[i] == 'L') x--;
		if(comandos[i] == 'R') x++;

		if(x == 0 && y == 0) max = i + 1;

	}

	printf("%d\n",max );

}
/* 1512826573931 */

#include<stdio.h>

int main(){
	int s;
	scanf(" %d",&s);
	int ans = 0;
	int x = 0, y = 0;
	for(int i = 0; i < s;++i){
		char a;
		scanf(" %c",&a);
		if(a == 'R')
			++x;
		if(a == 'U')
			++y;
		if(a == 'D')
			--y;
		if(a == 'L')
			--x;
		if(x == 0 && y == 0)
			ans = i+1;
	}
	printf("%d\n",ans);
	return 0;
}

#include<stdio.h>

char vec[1001];

int main() {
	int n, x = 0, y = 0;
	int temp = 0;
	scanf("%d", &n);

	scanf("%s", vec);

	for (int i = 0; i < n; i++) {
		if (vec[i] == 'L') x--;
		if (vec[i] == 'R') x++;
		if (vec[i] == 'D') y--;
		if (vec[i] == 'U') y++;

		if (x == 0 && y == 0) temp = i + 1;
	}
	if (temp == 0)
		printf("0");
	else
		printf("%d", temp);
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int n,i,x,y,count,max;count=max=0;char s[500];x=y=0;
scanf("%d",&n);
scanf("%s",s);

for(i=0;i<strlen(s);i++)
{
if(s[i]=='U')
{
y=y+1;
if((x==0)&&(y==0)){
count=i+1;
if(max<count)
max=count;}}

else if(s[i]=='D'){
y=y-1;
if((x==0)&&(y==0)){
count=i+1;
if(max<count)
max=count;}}

else if(s[i]=='L'){
x=x-1;
if((x==0)&&(y==0)){
count=i+1;
if(max<count)
max=count;}}

else if(s[i]=='R'){
x=x+1;
if((x==0)&&(y==0)){
count=i+1;
if(max<count)
max=count;}}
}

printf("%d\n",max);
exit(0);
}

#include<stdio.h>

int main(){

int n;

scanf("%d",&n);

char command[101];

scanf("%s",command);

int i;
int max=0;
int x=0;int y=0;
for(i=0;i<n;i++){

	if(command[i]=='U'){

		y++;
	}

	if(command[i]=='D'){

                y--;
        }

	if(command[i]=='L'){

                x--;
        }

	if(command[i]=='R'){

                x++;
        }

	if(x==0&&y==0)
                max=i+1;

}

printf("%d\n",max);

return 0;
}

#include <stdio.h>

char s[110];

int main() {
	int n, i;
	int x = 0;
	int y = 0;
	int o = 0;
	scanf("%d", &n);
	scanf("%s", s);
	for (i=0; i<n; i++) {
		if (s[i] == 'L') {
			x--;
		} else if (s[i] == 'R') {
			x++;
		} else if (s[i]=='U') {
			y++;
		} else if (s[i]=='D') {
			y--;
		}
	}
	o = n-x+y;
	printf("%d\n", o);
	return 0;
}

#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
	int pos_x = 0, pos_y = 0;

	int n;
	scanf("%d", &n);

	getchar();

	char comando[100];
	scanf("%[^\n]", comando);

	int i;
	for (i = 0; i < n; i++)
	{
		if (comando[i] == 'U') pos_y++;
		else if (comando[i] == 'D') pos_y--;
		else if (comando[i] == 'R') pos_x++;
		else if (comando[i] == 'L') pos_x--;
	}

	printf("%d", (n - pos_x - pos_y) );

	return 0;
}

#include <stdio.h>

#define MAX_COMMANDS 100

int		main(void)
{
	char commands[MAX_COMMANDS + 1];
	int num_commands;
	int max_poss_commands;
	int lr_dist;
	int ud_dist;

	commands[MAX_COMMANDS] = '\0';

	scanf("%d\n", &num_commands);
	for (int i = 0; i < num_commands; i++)
		scanf("%c", &commands[i]);

	lr_dist = 0;
	ud_dist = 0;
	max_poss_commands = 0;
	for (int i = 0; i < num_commands; i++)
	{
		if (commands[i] == 'L')
			lr_dist--;
		else if (commands[i] == 'R')
			lr_dist++;
		else if (commands[i] == 'U')
			ud_dist++;
		else if (commands[i] == 'D')
			ud_dist--;

		if (lr_dist == 0 && ud_dist == 0)
			max_poss_commands = i + 1;
	}
	printf("%d\n", max_poss_commands);
	return (0);
}

#include <stdio.h>
#include <string.h>

int main()
{

int x = 0, y = 0, aux = 0, cont = 0, rep, i;
scanf("%d", &rep);
char comando[100];
scanf(" %s", comando);

	for(i = 0; i < rep; i++)
	{
		if(comando[i] == 'U') y++;
		else if(comando[i] == 'D') y--;
		else if(comando[i] == 'L') x--;
		else x++;
//  printf("x = %d, y = %d\n", x, y);
		if(x == 0 && y == 0){
//		puts("entrou");
		cont++;
		if(cont > aux){
		aux = cont;
		cont = 0;
		}
		}
		else cont ++;
	}

printf("%d", aux);

return 0;

}

/* 1512829528286 */

#include <stdio.h>
#include <math.h>

int main() {
    int n;
    scanf("%d", &n);
    int ct;
    int right, left, up, down;
    right = left = up = down = 0;
    for (ct = 0; ct <= n; ct++) {
        char temp;
        scanf("%c", &temp);
        if(temp == 'U'){
            up++;
        }
        if(temp == 'L'){
            left++;
        }
        if(temp == 'R'){
            right++;
        }
        if(temp == 'D'){
            down++;
        }
    }

    int minx, miny;
    if(right == left){
        minx = right + down;
    }else{
        minx = (right < left) ? right : left;
    }
    if(up == down){
        miny = up + down;
    }else {
        miny = (up < down) ? up : down;
    }
    printf("%d", minx + miny);

}

#include<stdio.h>
#include<string.h>
int main()
{
    char s[105];
    int i,n,x,xx,y,yy,cx,cy,total;
    while(~scanf("%d",&n))
    {
        getchar();
        scanf("%s",s);
        x=0;
        y=0;
        for(i=0;i<n;i++)
        {
            if(s[i]=='R' || s[i]=='L')
            {
                x++;
            }
            else if(s[i]=='U' || s[i]=='D')
            {
                y++;
            }
        }
        cx=0;
        cy=0;
        for(i=0;i<n;i++)
        {
            if(s[i]=='R')
            {
                cx=cx+1;
            }
            else if(s[i]=='L')
            {
                cx=cx-1;
            }
            else if(s[i]=='U')
            {
                cy=cy+1;
            }
            else if(s[i]=='D')
            {
                cy=cy-1;
            }
        }
        printf("%d %d",cx,cy);
        xx=x-cx;
        yy=y-cy;
        if(xx>n)
        {
            xx=xx%n;
        }
        if(yy>n)
        {
            yy=yy%n;
        }
        total=xx+yy;
        printf("%d\n",total);
    }
    return 0;
}

#include <stdio.h>

int main(void) {
	int n;
	scanf("%d", &n);
	char udlr;
	int xf = 0;
	int yf = 0;
	for(int i = 0; i < n; ++i){
		scanf(" %c", &udlr);
		if(udlr == 'U'){
			++yf;
		}
		else if(udlr == 'D'){
			--yf;
		}
		else if(udlr == 'R'){
			++xf;
		}
		else if(udlr == 'L'){
			--xf;
		}
	}
	printf("%d\n", n-xf-yf);
	return 0;
}

/* 1515093055897 */

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int Buggy_Robot(int n, char seq[])
{
    int i,u=0,d=0,l=0,r=0;
    for (i=0;i<n;i++)
    {
        if (seq[i]=='U')
                u+=1;
        else if (seq[i]=='D')
                 d+=1;
             else if (seq[i]=='L')
                  l+=1;
                else
                     r+=1;

        if (u==d && l==r )
            break;
    }
    if (i<n)
        return i+1;
    else
        return 0;

}
int main()
{
    int n;
    char seq[100];
    scanf("%d",&n);
    scanf("%s",seq);
    printf("%d",(Buggy_Robot(n,seq)));

    return 0;
}

#include <stdlib.h>
#include <stdio.h>

int main()
{
    int n, count = 0, L = 0, U = 0 , D = 0, R = 0, i;

    scanf("%d", &n);

    for(i = 0; i<n; i++)
    {
        char c;
        scanf("%c", &c);
        switch(c)
        {
            case 'L':
                L++;
                break;
            case 'D':
                D++;
                break;
            case 'U':
                U++;
                break;
            case 'R':
                R++;
                break;
        }
    }
    count = L<R?L:R + U<D?U:D;
    printf("%d", count);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int Buggy_Robot(int n, char seq[])
{
    int i,u=0,d=0,l=0,r=0;
    for (i=0;i<n;i++)
    {
        if (seq[i]=='U')
                u+=1;
        else if (seq[i]=='D')
                 d+=1;
             else if (seq[i]=='L')
                  l+=1;
                else
                     r+=1;

        if (u==d && l==r )
            break;
    }
    if (i<n)
        return i+1;
    else
        return 0;

}
int main()
{
    int n;
    char seq[101];
    scanf("%d",&n);
    scanf("%s",seq);
    printf("%d",(Buggy_Robot(n,seq)));

    return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
    char s[105];
    int i,n,x,xx,y,yy,cx,cy,total;
    while(~scanf("%d",&n))
    {
        getchar();
        scanf("%s",s);
        x=0;
        y=0;
        for(i=0;i<n;i++)
        {
            if(s[i]=='R' || s[i]=='L')
            {
                x++;
            }
            else if(s[i]=='U' || s[i]=='D')
            {
                y++;
            }
        }
        cx=0;
        cy=0;
        for(i=0;i<n;i++)
        {
            if(s[i]=='R')
            {
                cx=cx+1;
            }
            else if(s[i]=='L')
            {
                cx=cx-1;
            }
            else if(s[i]=='U')
            {
                cy=cy+1;
            }
            else if(s[i]=='D')
            {
                cy=cy-1;
            }
        }
        xx=x-cx;
        yy=y-cy;
        if(xx>n)
        {
            xx=xx%n;
        }
        if(yy>n)
        {
            yy=yy%n;
        }
        total=xx+yy;
        printf("%d\n",total);
    }
    return 0;
}

#include<stdio.h>

int main()
{

	int n,i;
	int lcount=0,rcount=0,dcount=0,ucount=0,count=0;

	char a[105];

	scanf("%d",&n);

	for(i=0;i<n;i++)
	{
		scanf("%c",&a[i]);

		if(a[i]=='L')lcount++;

		if(a[i]=='R')rcount++;

		if(a[i]=='U')ucount++;

		if(a[i]=='D')dcount++;
	}

		if(lcount<rcount)
			count+=2*lcount;
		else
			count+=2*rcount;

		if(dcount<ucount)
			count+=2*dcount;
		else
			count+=2*ucount;

	printf("%d",count);

	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char str[10000];

int main(){
int n,i,j,k,l,d,u,r,m,x=0,y=0,L;
l=0;d=0;r=0;u=0;
scanf("%d%s",&n,str);
L = strlen(str);

for(i=0;i<L;i++){
if(str[i]=='U')
y++;
else if(str[i]=='D')
y--;
else if(str[i]=='L')
x--;
else if(str[i]=='R')
x++;
}
int count = n-x-y;
printf("%d\n",count);
return 0;
}

#include <stdio.h>
#include <stdlib.h>

int x = 0, y = 0, n, i, s;
char d[3000];

int main()
{
    scanf("%d", &n);
    for (i = 0; i <= n; i++) scanf("%c", &d[i]);
    s = 0;
    for (i = 1; i <= n; i++)
    {
        switch(d[i])
        {
            case 'U' : y++; break;
            case 'D' : y--; break;
            case 'L' : x--; break;
            case 'R' : x++; break;
        }
        if ((x == 0) && (y == 0)) s = i;
    }
    printf("%d", s);
    return 0;
}

#include<stdio.h>

char vec[1001];

int main() {
	int n, x = 0, y = 0;
	int temp = 0;
	scanf("%d", &n);

	scanf("%s", vec);

	for (int i = 0; i < n; i++) {
		if (vec[i] == 'L') x--;
		if (vec[i] == 'R') x++;
		if (vec[i] == 'D') y--;
		if (vec[i] == 'U') y++;

		if (x == 0 && y == 0) temp = i + 1;
	}
	if (temp == 0)
		printf("0");
	else
		printf("%d", temp);
}

#include <stdio.h>

int main()
{
    int i, n, x, y, t;
    char c[101] = { 0 };

    scanf("%d ", &n);

    scanf("%s ", c);

    x = 0;
    y = 0;
    t = 0;
    for (i = 0; i < n; ++i) {
        switch (c[i]) {
            case 'U':
                y += 1;
                break;
            case 'D':
                y -= 1;
                break;
            case 'L':
                x -= 1;
                break;
            case 'R':
                x += 1;
                break;
            default:
                printf("what");
        }

        if (x == 0 && y == 0) {
            t = i + 1;
        }
    }

    printf("%d\n", t);

    return 0;
}

#include <stdio.h>

int main(){
  int n; scanf("%d ", &n);

  int x=0, y=0, cnt=0, ans=0;
  while(n--){
    char ch;
    ch = getchar();
    if (ch=='U') y++;
    else if (ch=='D') y--;
    else if (ch=='L') x--;
    else if (ch=='R') x++;
    cnt++, ans++;
    if( x==0 && y==0){
      cnt=0;
    }
  }
  printf("%d\n", ans-cnt<0?0:ans-cnt);
}

#include <stdio.h>
#include <stdlib.h>

int x = 0, y = 0, n, i, s;
char d[101];

int main()
{
    scanf("%d", &n);
    if ((n >= 1) && (n <= 100))
    {
        scanf("\n");
        for (i = 1; i <= n; i++) scanf("%c", &d[i]);
        s = 0;
        for (i = 1; i <= n; i++)
        {
            switch(d[i])
            {
                case 'U' : y++; break;
                case 'D' : y--; break;
                case 'L' : x--; break;
                case 'R' : x++; break;
            }
            if ((x == 0) && (y == 0)) s = i;
        }
        printf("%d", s);
    }
    return 0;
}

#include <stdio.h>
int com[150];
char s[1005];

int min(int a, int b)
{
    return a < b ? a : b;
}

int main()
{
    int n, i, tot;
    scanf("%d%s", &n, s);
    for(i = 1000; i < n; ++i)
        ++com[s[i]];
    tot = 2 * (min(com['U'], com['D']) + min(com['L'], com['R']));
    printf("%d\n", tot);
    return 0;
}

#include <stdio.h>
#include <string.h>

int main()
{
    int n,op=0;
    scanf("%d\n",&n);
    char x[n];
     gets(x);
    int i,u=0,l=0,d=0,r=0;
    for(i=0;i<n;i++)
    {
        switch(x[i])
        {
          case 'U':
            u++;
            break;
          case 'u' :
            u++;
            break;
          case 'L' :
            l++;
            break;
          case 'l' :
            l++;
            break;
          case 'd' :
            d++;
            break;
          case 'D' :
            d++;
            break;
          case 'r' :
            r++;
            break;
          case 'R' :
            r++;
            break;
        }
    }
    if (u>=d)
        op=2*d;
    if (r>=l)
        op+=2*l;
    printf("%d",op);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int x = 0, y = 0, n, i, s;
char d[101];

int main()
{
    scanf("%d", &n);
    for (i = 0; i <= n; i++) scanf("%c", &d[i]);
    s = 0;
    for (i = 1; i <= n; i++)
    {
        switch(d[i])
        {
            case 'U' : y++; break;
            case 'D' : y--; break;
            case 'L' : x--; break;
            case 'R' : x++; break;
        }
        if ((x == 0) && (y == 0)) s = i;
    }
    printf("%d", s);
    return 0;
}


/* practice with Dukkha */
#include <stdio.h>

#define N	5

int max(int a, int b) { return a > b ? a : b; }

double prob(int l, int r, int a) {
	return (double) max(r - max(l, a) + 1, 0) / (r - l + 1);
}

int main() {
	static int ll[N], rr[N];
	int n, i, j, a;
	double ans, p, q;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d%d", &ll[i], &rr[i]);
	ans = 0;
	for (a = 1; a <= 10000; a++) {
		p = 1;
		for (j = 0; j < n; j++)
			p *= 1 - prob(ll[j], rr[j], a);
		q = p;
		for (i = 0; i < n; i++) {
			p = prob(ll[i], rr[i], a);
			for (j = 0; j < n; j++)
				if (j != i)
					p *= 1 - prob(ll[j], rr[j], a);
			q += p;
		}
		ans += 1 - q;
	}
	printf("%.9f\n", ans);
	return 0;
}

#include <stdio.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))

int main(void) {
    int i;
    int n;
    int l[5], r[5];
    int val;
    long long cnt[10001];
    double sum, ans;

    scanf("%d", &n);
    for (i = 0; i < n; i++)
        scanf("%d %d", l + i, r + i);

    sum = 0.0;
    for (val = 1; val <= 10000; val++) {
        int high1, high2, high3, high4, high5;
        int sec1, sec2, sec3, sec4;

        cnt[val] = 0;

        // high1パターン
        //  sec1 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                if (high1 != sec1) {
                    long long way;

                    way = 1LL;
                    for (i = 0; i < n; i++)
                        if (i == high1)
                            way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                        else if (i == sec1)
                            way *= val >= l[i] && val <= r[i];
                        else
                            way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                    cnt[val] += way;
                }
        //  sec2 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                for (sec2 = sec1 + 1; sec2 < n; sec2++)
                    if (high1 != sec1 && high1 != sec2) {
                        long long way;

                        way = 1LL;
                        for (i = 0; i < n; i++)
                            if (i == high1)
                                way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                            else if (i == sec1 || i == sec2)
                                way *= val >= l[i] && val <= r[i];
                            else
                                way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                        cnt[val] += way;
                    }
        //  sec3 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                for (sec2 = sec1 + 1; sec2 < n; sec2++)
                    for (sec3 = sec2 + 1; sec3 < n; sec3++)
                        if (high1 != sec1 && high1 != sec2 && high1 != sec3) {
                            long long way;

                            way = 1LL;
                            for (i = 0; i < n; i++)
                                if (i == high1)
                                    way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                                else if (i == sec1 || i == sec2 || i == sec3)
                                    way *= val >= l[i] && val <= r[i];
                                else
                                    way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                            cnt[val] += way;
                        }
        //  sec4 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                for (sec2 = sec1 + 1; sec2 < n; sec2++)
                    for (sec3 = sec2 + 1; sec3 < n; sec3++)
                        for (sec4 = sec3 + 1; sec4 < n; sec4++)
                            if (high1 != sec1 && high1 != sec2 && high1 != sec3 && high1 != sec4) {
                                long long way;

                                way = 1LL;
                                for (i = 0; i < n; i++)
                                    if (i == high1)
                                        way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                                    else if (i == sec1 || i == sec2 || i == sec3 || i == sec4)
                                        way *= val >= l[i] && val <= r[i];
                                    else
                                        way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                                cnt[val] += way;
                            }

        // high2パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++) {
                    long long way;

                    way = 1LL;
                    for (i = 0; i < n; i++)
                        if (i == high1 || i == high2)
                            way *= val >= l[i] && val <= r[i];
                        else
                            way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                    cnt[val] += way;
                }
        // high3パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++)
                for (high3 = high2 + 1; high3 < n; high3++) {
                        long long way;

                        way = 1LL;
                        for (i = 0; i < n; i++)
                            if (i == high1 || i == high2 || i == high3)
                                way *= val >= l[i] && val <= r[i];
                            else
                                way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                        cnt[val] += way;
                    }
        // high4パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++)
                for (high3 = high2 + 1; high3 < n; high3++)
                    for (high4 = high3 + 1; high4 < n; high4++) {
                            long long way;

                            way = 1LL;
                            for (i = 0; i < n; i++)
                                if (i == high1 || i == high2 || i == high3 || i == high4)
                                    way *= val >= l[i] && val <= r[i];
                                else
                                    way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                            cnt[val] += way;
                        }
        // high5パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++)
                for (high3 = high2 + 1; high3 < n; high3++)
                    for (high4 = high3 + 1; high4 < n; high4++)
                        for (high5 = high4 + 1; high5 < n; high5++) {
                                long long way;

                                way = 1LL;
                                for (i = 0; i < n; i++)
                                    if (i == high1 || i == high2 || i == high3 || i == high4 || i == high5)
                                        way *= val >= l[i] && val <= r[i];
                                    else
                                        way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                                cnt[val] += way;
                            }

        sum += cnt[val];
    }

    ans = 0.0;
    for (i = 1; i <= 10000; i++)
        ans += (double)i * cnt[i] / sum;

    printf("%.15f\n", ans);

    return 0;
}

#include <stdio.h>

#define MAX_COMP	5
#define MAX_BID		10000

int min(int x, int y)
{
	return (x <= y) ? x : y;
}

int max(int x, int y)
{
	return (x >= y) ? x : y;
}

int main(void)
{
	int n;
	int l[MAX_COMP];
	int r[MAX_COMP];
	int i, j, k, z;
	double p, q, u;

	scanf("%d", &n);
	for(i = n; i --;)
		scanf("%d %d", &l[i], &r[i]);

	p = 0.0;
	for(i = 1; i <= MAX_BID; i ++)
	{
		for(k = 0; k < n; k ++)
		{
			if(!(l[k] <= i && i <= r[k]))
				continue;

			q = 1 / (double)(r[k] - l[k] + 1);

			for(j = 0; j < n; j ++)
			{
				if(i >= r[j])
					continue;

				if(k == j)
					continue;

				u = q;
				u *= (r[j] - max(l[j], i + 1) + 1) / (double)(r[j] - l[j] + 1);

				for(z = 0; z < n; z ++)
				{
					if(z == j || z == k)
						continue;

					if(l[z] > i)
					{
						u = 0.0;
						break;
					}

					if(z > k)
						u *= (min(r[z], i) - l[z] + 1) / (double)(r[z] - l[z] + 1);
					else
						u *= (min(r[z], i - 1) - l[z] + 1) / (double)(r[z] - l[z] + 1);
				}

				p += i * u;
			}

			for(j = k + 1; j < n; j ++)
			{
				if(!(l[j] <= i && i <= r[j]))
					continue;

				if(k == j)
					continue;

				u = q;
				u *= 1 / (double)(r[j] - l[j] + 1);

				for(z = 0; z < n; z ++)
				{
					if(z == j || z == k)
						continue;

					if(l[z] > i)
					{
						u = 0.0;
						break;
					}

					if(z > k && z > j)
						u *= (min(r[z], i) - l[z] + 1) / (double)(r[z] - l[z] + 1);
					else
						u *= (min(r[z], i - 1) - l[z] + 1) / (double)(r[z] - l[z] + 1);
				}

				p += i * u;
			}
		}
	}

	printf("%.9lf\n", p);

	return 0;
}

#include <stdio.h>

double L[6], R[6];
int n;

int min(int a, double b_) {
    int b = b_;
    return (a < b ? a : b);
}
int max(int a, double b_) {
    int b = b_;
    return (a > b ? a : b);
}

double noHigherThan(int b) {
    int i, j, f;
    double p = 0.0, p1;
    for(i = 3; i < (1 << n); i++) {
        p1 = 1.0;
        f = 0;
        for(j = 0; j < n; j++) {
            if((i & (1 << j)) > 0) {
                if(!(L[j] <= b && b <= R[j])) {
                    p1 = 0;
                    break;
                }
                p1 *= (1.0 / (R[j] - L[j] + 1));
                f++;
            }
            else {
                if(L[j] >= b) {
                    p1 = 0;
                    break;
                }
                p1 *= ((min(b - 1, R[j]) - L[j] + 1) / (R[j] - L[j] + 1));
            }
        }
        if(f >= 2)
            p = p + p1;
    }
    return p;
}

double oneHigherThan(int b, int x) {
    if(b >= R[x]) return 0.0;
    double p = 0.0, p1;
    int i, j, f;
    for(i = 1; i < (1 << n); i++) {
        p1 = 1.0;
        f = 0;
        for(j = 0; j < n; j++) {
            if(j == x) continue;
            if((i & (1 << j)) > 0) {
                if(!(L[j] <= b && b <= R[j])) {
                    p1 = 0;
                    break;
                }
                p1 *= (1.0 / (R[j] - L[j] + 1));
                f++;
            }
            else {
                if(L[j] >= b) {
                    p1 = 0;
                    break;
                }
                p1 *= ((min(b - 1, R[j]) - L[j] + 1.0) / (R[j] - L[j] + 1.0));
            }
        }
        if(f == 0) p1 = 0;
        p = p + p1;
    }
    p /= 2.0;
    return p * ((R[x] - max(b + 1, L[x]) + 1.0) / (R[x] - L[x] + 1));
}

double P(int b) {
    double p = 0.0;
    p = noHigherThan(b);
    int i;
    for(i = 0; i < n; i++)
        p = p + oneHigherThan(b, i);
    return p;
}

int main(int argc, char const *argv[]) {
    int i, lim = 0;
    double ex = 0;
    scanf("%d", &n);
    for(i = 0; i < n; i++) {
        scanf("%lf%lf", &L[i], &R[i]);
        lim = max(lim, R[i]);
    }
    for(i = 1; i <= lim; i++) {
        ex = ex + i * P(i);
    }
    printf("%.10lf\n", ex);
    return 0;
}

#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>

int L[5];
int R[5];
int D[5];
double h[5],l[5],m[5];
int n,i,j;
double ans=0;
double f(int i, char mid,char higher){
	if(i==n)return (double)(mid+higher>1);
	double ret=0;
	if(!higher){
		ret+=h[i]*f(i+1,mid,1);
	}
	ret+=m[i]*f(i+1,mid+1,higher);
	ret+=l[i]*f(i+1,mid,higher);
	return ret;
}

int main(){
	scanf("%d",&n);
	for(i=0;i<n;i++)scanf("%d%d",L+i,R+i),D[i]=R[i]-L[i]+1;
	for(i=1;i<=10000;i++){
		for(j=0;j<n;j++){
			if(R[j]>i){
				if(L[j]>i){
					h[j]=1;
				}else{
					h[j]=(R[j]-i)/(double)D[j];
				}
			}else h[j]=0;
			if(R[j]>=i&&L[j]<=i){
				m[j]=1/(double)D[j];
			}else m[j]=0;

			if(L[j]<i){
				if(R[j]<i){
					l[j]=1;
				}else{
					l[j]=(i-L[j])/(double)D[j];
				}
			}else l[j]=0;
		}
		ans+=f(0,0,0)*i;
	}
	printf("%.12f",ans);
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))

int compare(const void *a, const void *b) {
    return *(long long *)a - *(long long *)b;
}

int main(void) {
    int i;
    int n;
    int l[5], r[5];
    int val;
    long long cnt[10001], tmp[10001];
    double sum, ans;

    scanf("%d", &n);
    for (i = 0; i < n; i++)
        scanf("%d %d", l + i, r + i);

    sum = 0.0;
    for (val = 1; val <= 10000; val++) {
        int high1, high2, high3, high4, high5;
        int sec1, sec2, sec3, sec4;

        cnt[val] = 0;

        // high1パターン
        //  sec1 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                if (high1 != sec1) {
                    long long way;

                    way = 1LL;
                    for (i = 0; i < n; i++)
                        if (i == high1)
                            way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                        else if (i == sec1)
                            way *= val >= l[i] && val <= r[i];
                        else
                            way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                    cnt[val] += way;
                }
        //  sec2 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                for (sec2 = sec1 + 1; sec2 < n; sec2++)
                    if (high1 != sec1 && high1 != sec2) {
                        long long way;

                        way = 1LL;
                        for (i = 0; i < n; i++)
                            if (i == high1)
                                way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                            else if (i == sec1 || i == sec2)
                                way *= val >= l[i] && val <= r[i];
                            else
                                way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                        cnt[val] += way;
                    }
        //  sec3 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                for (sec2 = sec1 + 1; sec2 < n; sec2++)
                    for (sec3 = sec2 + 1; sec3 < n; sec3++)
                        if (high1 != sec1 && high1 != sec2 && high1 != sec3) {
                            long long way;

                            way = 1LL;
                            for (i = 0; i < n; i++)
                                if (i == high1)
                                    way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                                else if (i == sec1 || i == sec2 || i == sec3)
                                    way *= val >= l[i] && val <= r[i];
                                else
                                    way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                            cnt[val] += way;
                        }
        //  sec4 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                for (sec2 = sec1 + 1; sec2 < n; sec2++)
                    for (sec3 = sec2 + 1; sec3 < n; sec3++)
                        for (sec4 = sec3 + 1; sec4 < n; sec4++)
                            if (high1 != sec1 && high1 != sec2 && high1 != sec3 && high1 != sec4) {
                                long long way;

                                way = 1LL;
                                for (i = 0; i < n; i++)
                                    if (i == high1)
                                        way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                                    else if (i == sec1 || i == sec2 || i == sec3 || i == sec4)
                                        way *= val >= l[i] && val <= r[i];
                                    else
                                        way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                                cnt[val] += way;
                            }

        // high2パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++) {
                    long long way;

                    way = 1LL;
                    for (i = 0; i < n; i++)
                        if (i == high1 || i == high2)
                            way *= val >= l[i] && val <= r[i];
                        else
                            way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                    cnt[val] += way;
                }
        // high3パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++)
                for (high3 = high2 + 1; high3 < n; high3++) {
                        long long way;

                        way = 1LL;
                        for (i = 0; i < n; i++)
                            if (i == high1 || i == high2 || i == high3)
                                way *= val >= l[i] && val <= r[i];
                            else
                                way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                        cnt[val] += way;
                    }
        // high4パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++)
                for (high3 = high2 + 1; high3 < n; high3++)
                    for (high4 = high3 + 1; high4 < n; high4++) {
                            long long way;

                            way = 1LL;
                            for (i = 0; i < n; i++)
                                if (i == high1 || i == high2 || i == high3 || i == high4)
                                    way *= val >= l[i] && val <= r[i];
                                else
                                    way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                            cnt[val] += way;
                        }
        // high5パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++)
                for (high3 = high2 + 1; high3 < n; high3++)
                    for (high4 = high3 + 1; high4 < n; high4++)
                        for (high5 = high4 + 1; high4 < n; high4++) {
                                long long way;

                                way = 1LL;
                                for (i = 0; i < n; i++)
                                    if (i == high1 || i == high2 || i == high3 || i == high4 || i == high5)
                                        way *= val >= l[i] && val <= r[i];
                                    else
                                        way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                                cnt[val] += way;
                            }

        sum += cnt[val];
    }

    memcpy(tmp, cnt, sizeof cnt);
    qsort(tmp, 10001, sizeof(long long), &compare);

    sum = 0.0;
    for (i = 0; i <= 10000; i++)
        sum += tmp[i];

    ans = 0.0;
    for (i = 1; i <= 10000; i++)
        ans += (double)i * cnt[i] / sum;

    printf("%.15f\n", ans);

    return 0;
}

#include <stdio.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))

int main(void) {
    int i;
    int n;
    int l[5], r[5];
    int val;
    long long cnt[10001];
    double sum, ans;

    scanf("%d", &n);
    for (i = 0; i < n; i++)
        scanf("%d %d", l + i, r + i);

    sum = 0.0;
    for (val = 1; val <= 10000; val++) {
        int high1, high2, high3, high4, high5;
        int sec1, sec2, sec3, sec4;

        cnt[val] = 0;

        // high1パターン
        //  sec1 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                if (high1 != sec1) {
                    long long way;

                    way = 1LL;
                    for (i = 0; i < n; i++)
                        if (i == high1)
                            way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                        else if (i == sec1)
                            way *= val >= l[i] && val <= r[i];
                        else
                            way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                    cnt[val] += way;
                }
        //  sec2 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                for (sec2 = sec1 + 1; sec2 < n; sec2++)
                    if (high1 != sec1 && high1 != sec2) {
                        long long way;

                        way = 1LL;
                        for (i = 0; i < n; i++)
                            if (i == high1)
                                way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                            else if (i == sec1 || i == sec2)
                                way *= val >= l[i] && val <= r[i];
                            else
                                way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                        cnt[val] += way;
                    }
        //  sec3 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                for (sec2 = sec1 + 1; sec2 < n; sec2++)
                    for (sec3 = sec2 + 1; sec3 < n; sec3++)
                        if (high1 != sec1 && high1 != sec2 && high1 != sec3) {
                            long long way;

                            way = 1LL;
                            for (i = 0; i < n; i++)
                                if (i == high1)
                                    way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                                else if (i == sec1 || i == sec2 || i == sec3)
                                    way *= val >= l[i] && val <= r[i];
                                else
                                    way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                            cnt[val] += way;
                        }
        //  sec4 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                for (sec2 = sec1 + 1; sec2 < n; sec2++)
                    for (sec3 = sec2 + 1; sec3 < n; sec3++)
                        for (sec4 = sec3 + 1; sec4 < n; sec4++)
                            if (high1 != sec1 && high1 != sec2 && high1 != sec3 && high1 != sec4) {
                                long long way;

                                way = 1LL;
                                for (i = 0; i < n; i++)
                                    if (i == high1)
                                        way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                                    else if (i == sec1 || i == sec2 || i == sec3 || i == sec4)
                                        way *= val >= l[i] && val <= r[i];
                                    else
                                        way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                                cnt[val] += way;
                            }

        // high2パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++) {
                    long long way;

                    way = 1LL;
                    for (i = 0; i < n; i++)
                        if (i == high1 || i == high2)
                            way *= val >= l[i] && val <= r[i];
                        else
                            way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                    cnt[val] += way;
                }
        // high3パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++)
                for (high3 = high2 + 1; high3 < n; high3++) {
                        long long way;

                        way = 1LL;
                        for (i = 0; i < n; i++)
                            if (i == high1 || i == high2 || i == high3)
                                way *= val >= l[i] && val <= r[i];
                            else
                                way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                        cnt[val] += way;
                    }
        // high4パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++)
                for (high3 = high2 + 1; high3 < n; high3++)
                    for (high4 = high3 + 1; high4 < n; high4++) {
                            long long way;

                            way = 1LL;
                            for (i = 0; i < n; i++)
                                if (i == high1 || i == high2 || i == high3 || i == high4)
                                    way *= val >= l[i] && val <= r[i];
                                else
                                    way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                            cnt[val] += way;
                        }
        // high5パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++)
                for (high3 = high2 + 1; high3 < n; high3++)
                    for (high4 = high3 + 1; high4 < n; high4++)
                        for (high5 = high4 + 1; high4 < n; high4++) {
                                long long way;

                                way = 1LL;
                                for (i = 0; i < n; i++)
                                    if (i == high1 || i == high2 || i == high3 || i == high4 || i == high5)
                                        way *= val >= l[i] && val <= r[i];
                                    else
                                        way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                                cnt[val] += way;
                            }

        sum += cnt[val];
    }

    ans = 0.0;
    for (i = 1; i <= 10000; i++)
        ans += (double)i * cnt[i] / sum;

    printf("%.15f\n", ans);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))

int compare(const void *a, const void *b) {
    return *(long long *)a - *(long long *)b;
}

int main(void) {
    int i;
    int n;
    int l[5], r[5];
    int val;
    long long cnt[10001], tmp[10001];
    double sum, ans;

    scanf("%d", &n);
    for (i = 0; i < n; i++)
        scanf("%d %d", l + i, r + i);

    sum = 0.0;
    for (val = 1; val <= 10000; val++) {
        int high1, high2, high3, high4, high5;
        int sec1, sec2, sec3, sec4;

        cnt[val] = 0;

        // high1パターン
        //  sec1 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                if (high1 != sec1) {
                    long long way;

                    way = 1LL;
                    for (i = 0; i < n; i++)
                        if (i == high1)
                            way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                        else if (i == sec1)
                            way *= val >= l[i] && val <= r[i];
                        else
                            way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                    cnt[val] += way;
                }
        //  sec2 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                for (sec2 = sec1 + 1; sec2 < n; sec2++)
                    if (high1 != sec1 && high1 != sec2) {
                        long long way;

                        way = 1LL;
                        for (i = 0; i < n; i++)
                            if (i == high1)
                                way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                            else if (i == sec1 || i == sec2)
                                way *= val >= l[i] && val <= r[i];
                            else
                                way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                        cnt[val] += way;
                    }
        //  sec3 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                for (sec2 = sec1 + 1; sec2 < n; sec2++)
                    for (sec3 = sec2 + 1; sec3 < n; sec3++)
                        if (high1 != sec1 && high1 != sec2 && high1 != sec3) {
                            long long way;

                            way = 1LL;
                            for (i = 0; i < n; i++)
                                if (i == high1)
                                    way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                                else if (i == sec1 || i == sec2 || i == sec3)
                                    way *= val >= l[i] && val <= r[i];
                                else
                                    way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                            cnt[val] += way;
                        }
        //  sec4 パターン
        for (high1 = 0; high1 < n; high1++)
            for (sec1 = 0; sec1 < n; sec1++)
                for (sec2 = sec1 + 1; sec2 < n; sec2++)
                    for (sec3 = sec2 + 1; sec3 < n; sec3++)
                        for (sec4 = sec3 + 1; sec4 < n; sec4++)
                            if (high1 != sec1 && high1 != sec2 && high1 != sec3 && high1 != sec4) {
                                long long way;

                                way = 1LL;
                                for (i = 0; i < n; i++)
                                    if (i == high1)
                                        way *= max(r[i] - max(l[i], val + 1) + 1, 0);
                                    else if (i == sec1 || i == sec2 || i == sec3 || i == sec4)
                                        way *= val >= l[i] && val <= r[i];
                                    else
                                        way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                                cnt[val] += way;
                            }

        // high2パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++) {
                    long long way;

                    way = 1LL;
                    for (i = 0; i < n; i++)
                        if (i == high1 || i == high2)
                            way *= val >= l[i] && val <= r[i];
                        else
                            way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                    cnt[val] += way;
                }
        // high3パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++)
                for (high3 = high2 + 1; high3 < n; high3++) {
                        long long way;

                        way = 1LL;
                        for (i = 0; i < n; i++)
                            if (i == high1 || i == high2 || i == high3)
                                way *= val >= l[i] && val <= r[i];
                            else
                                way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                        cnt[val] += way;
                    }
        // high4パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++)
                for (high3 = high2 + 1; high3 < n; high3++)
                    for (high4 = high3 + 1; high4 < n; high4++) {
                            long long way;

                            way = 1LL;
                            for (i = 0; i < n; i++)
                                if (i == high1 || i == high2 || i == high3 || i == high4)
                                    way *= val >= l[i] && val <= r[i];
                                else
                                    way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                            cnt[val] += way;
                        }
        // high5パターン
        for (high1 = 0; high1 < n; high1++)
            for (high2 = high1 + 1; high2 < n; high2++)
                for (high3 = high2 + 1; high3 < n; high3++)
                    for (high4 = high3 + 1; high4 < n; high4++)
                        for (high5 = high4 + 1; high4 < n; high4++) {
                                long long way;

                                way = 1LL;
                                for (i = 0; i < n; i++)
                                    if (i == high1 || i == high2 || i == high3 || i == high4 || i == high5)
                                        way *= val >= l[i] && val <= r[i];
                                    else
                                        way *= max(min(r[i], val - 1) - l[i] + 1, 0);

                                cnt[val] += way;
                            }

        sum += cnt[val];
    }

    memcpy(tmp, cnt, sizeof cnt);
    qsort(tmp, 10001, sizeof(long long), &compare);

    sum = 0.0;
    for (i = 0; i <= 10000; i++)
        sum += tmp[i];

    sum = 1.0;
    for (i = 0; i < n; i++)
        sum *= (double)(r[i] - l[i] + 1);

    ans = 0.0;
    for (i = 1; i <= 10000; i++)
        ans += (double)i * cnt[i] / sum;

    printf("%.15f\n", ans);

    return 0;
}

#include <stdio.h>

#define MAX_COMP	5
#define MAX_BID		10000

int min(int x, int y)
{
	return (x <= y) ? x : y;
}

int max(int x, int y)
{
	return (x >= y) ? x : y;
}

int main(void)
{
	int n;
	int l[MAX_COMP];
	int r[MAX_COMP];
	int i, j, k, z;
	double p, q, u;

	scanf("%d", &n);
	for(i = n; i --;)
		scanf("%d %d", &l[i], &r[i]);

	p = 0.0;
	for(i = 1; i <= MAX_BID; i ++)
	{
		for(k = 0; k < n; k ++)
		{
			if(!(l[k] <= i && i <= r[k]))
				continue;

			q = 1 / (double)(r[k] - l[k] + 1);

			for(j = 0; j < n; j ++)
			{
				if(i >= r[j])
					continue;

				if(k == j)
					continue;

				u = q;
				u *= (r[j] - max(l[j], i + 1) + 1) / (double)(r[j] - l[j] + 1);

				for(z = 0; z < n; z ++)
				{
					if(z == j || z == k)
						continue;

					if(l[z] > i)
					{
						u = 0.0;
						break;
					}

					if(z > k)
						u *= (min(r[z], i) - l[z] + 1) / (double)(r[z] - l[z] + 1);
					else
						u *= (min(r[z], i - 1) - l[z] + 1) / (double)(r[z] - l[z] + 1);
				}

				p += i * u;
			}

			for(j = k + 1; j < n; j ++)
			{
				if(!(l[j] <= i && i <= r[j]))
					continue;

				if(k == j)
					continue;

				u = q;
				u *= 1 / (double)(r[j] - l[j] + 1);

				for(z = 0; z < n; z ++)
				{
					if(z == j || z == k)
						continue;

					if(l[z] > i)
					{
						u = 0.0;
						break;
					}

					if(z > k && z > j)
						u *= (min(r[z], i) - l[z] + 1) / (double)(r[z] - l[z] + 1);
					else
						u *= (min(r[z], i - 1) - l[z] + 1) / (double)(r[z] - l[z] + 1);
				}

				p += i * u;
			}
		}
	}

	printf("%lf\n", p);

	return 0;
}