how have u got the input...u must be having some counter which tracks the number of inputs..why not try using the same counter for the output!!